Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Trouble with getX() and getY()

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Trouble with getX() and getY()

    Hi all,

    I'm new to programming and am trying to understand getX() and getY() to extract the x, y coordinates from a point entered by the user. One of my problems is that I've defined that point (location) as a double, and so I'm getting the error 'double cannot be dereferenced' when I call location.getX() and location.getY(). To clarify, the x, y is a point of 2 integers.

    This program is supposed to take the name, direction, and location of a robot and depending on the user cmd's, it's supposed to move it forward one step, turn it left (N,S,W,E), and print a report saying what was done.

    I'd appreciate any clarification so I can test the code I've got. Thanks in advance

    import java.util.*;
     
    class Robot {
      //method main gets the values from the user
      public static void main(String[] args) {
        Scanner zip = new Scanner(System.in);
        System.out.println("Enter the robot's name: ");
        String name = zip.nextLine();
        System.out.println("Enter the location (x, y) format: ");
        double location = zip.nextDouble();
        System.out.println("Enter the direction the robot is facing: ");
        String direction = zip.nextLine();
        double x = location.getX();
        double y = location.getY();
     
      }
      //method turnLeft changes the direction depending on the currentLocation
      String turnLeft(String direction) { 
        String currentLocation = direction;
        if (currentLocation == "North") {
          currentLocation = "West"; }
        if (currentLocation == "West") {
          currentLocation = "South"; }
        if (currentLocation == "South") {
          currentLocation = "East"; }
        if (currentLocation == "East") {
          currentLocation = "North"; }
      }
      //method moveForward changes the location depending on the currentLocation
      String moveForward(String direction, Double location) { 
        String currentLocation = direction;
        if (currentLocation == "South") {
          location.getY() = location.get(Y) + 1; }
        if (currentLocation == "West") {
          location.getX() = location.getX() - 1; }
      }
      //method report prints the following: 
      String report(String direction, Double location) {
        return name + ", " + location.getX() + ", " + location.getY() + ", " + currentLocation; 
     
      }
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble with getX() and getY()

    I can't find a getX() method for the primitive type double.

    Also, I wonder if it's an integer/double mixup.

    Am looking at it now.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble with getX() and getY()

    Yep, confirmed that there's no Double method called getX().

    Just tried it in a test sample and got the "double cannot be dereferenced" thing too.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with getX() and getY()

    Do you know what the input should be in the (x, y) form so that I can use getX()?

    I tried changing it to integer but the input is not just one integer, it's a pair of integers.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble with getX() and getY()

    Well, you could just ask the user for an x value, store it, then ask for a y value and store it.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble with getX() and getY()

    Also, your moveForeward() and turnLeft() methods should probably be void methods according to the comments before the methods.

    The method isn't returning any value though you have set the return type for them as a void.

    This has nothing really to do with the point thing, but I just noticed it.

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble with getX() and getY()

    Also, you can use double values as input, just so long as you use two of them.

    I'm working on the code right now to fix it.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with getX() and getY()

        System.out.println("Enter the location (x, y) format: ");
        double location = zip.nextDouble();
    A double can only hold one value. For example: 1.23234. Not 3 and 4
    Add a println("loc=" + location);
    following the above code to see what the variable location holds after you enter the input.

    You are going to need read in two times, once for x and once for y.
    If you were to read a String: "3 , 4" then you could use some other methods to get the separate x and y values from the String. See the split() method.

    currentLocation == "North"
    Use the equals() method to compare Strings. The == operator is for primitives.
    Last edited by Norm; July 11th, 2011 at 07:43 PM.

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble with getX() and getY()

    I've gotten it this far, though I've no idea what you plan to do with the variable currentLocation;

    As it is, it will compile, but nothing in the direction will change.

    I can't get any further unless I know what to do with currentLocation.

    I've gotten rid of all, I think, scope problems.

    However, the currentLocation thing had been going out of scope so I tweaked a few things but don't think I solved the problem as I don't know what you intended to do with that variable. Is it the old location or the new one?

        import java.util.*;
     
       class Robot {
          static double locationX, locationY;
          static String name;
          static String direction;
     
     
     
     
       //method main gets the values from the user
          public static void main(String[] args) {
             Scanner zip = new Scanner(System.in);
             System.out.println("Enter the robot's name: ");
             name = zip.nextLine();
             setName(name);
             System.out.println("Enter the direction the robot is facing: ");
              direction = zip.nextLine();
             setDirection(direction);
             System.out.println("Enter the location (x, y) format: ");
             locationX = zip.nextDouble();
             locationY = zip.nextDouble();
             setLocationX(locationX);
             setLocationY(locationY);
     
     
             moveForward(direction);
             turnLeft(direction);
     
             System.out.println(report(direction));
             System.out.println(direction);
     
     
          }
       //method turnLeft changes the direction depending on the currentLocation
          static String turnLeft(String direction) { 
             String currentLocation = direction;
             if (currentLocation.equals ("North")) {
                currentLocation = "West"; }
             else if (currentLocation.equals("West")) {
                currentLocation = "South"; }
             else  if (currentLocation.equals("South")) {
                currentLocation = "East"; }
             else {
                currentLocation = "North"; }
             return currentLocation;
          }
       //method moveForward changes the location depending on the currentLocation
          static String moveForward(String direction) { 
             String currentLocation = direction;
             if (currentLocation.equals("South") ){
                setLocationY( getLocationY() + 1); }
             if (currentLocation.equals("West")) {
                setLocationX(getLocationX() - 1); }
             return currentLocation;
     
          }
       //method report prints the following: 
          static  String report(String direction) {
             String str =     getName() + ", " + getLocationX() + ", " + getLocationY() + ", " + moveForward(direction); 
             return (str);
          }
     
          public static void setName(String name2)
          {
             name = name2;
          }
     
          public static String getName()
          {
             return name;
          }
     
          public static void setLocationX(double locationX2)
          {
             locationX = locationX2;
          }
     
          public static double getLocationX()
          {
             return locationX;
          }
     
          public static void setLocationY(double locationY2)
          {
             locationY = locationY2;
          }
     
          public static double getLocationY()
          {
             return locationY;
          }
     
          public static void setDirection(String direction2)
          {
             direction = direction2;
          }
     
          public static String getDirection()
          {
             return direction;
          }
       }

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble with getX() and getY()

    Ok, this is probably like nearer to what you wanted, right?

        import java.util.*;
     
       class Robot {
          static double locationX, locationY;
          static String name;
          static String direction;
     
     
     
     
       //method main gets the values from the user
          public static void main(String[] args) {
             Scanner zip = new Scanner(System.in);
             System.out.println("Enter the robot's name: ");
             name = zip.nextLine();
             setName(name);
             System.out.println("Enter the direction the robot is facing: ");
             direction = zip.nextLine();
             setDirection(direction);
             System.out.println("Enter the location (x, y) format: ");
             locationX = zip.nextDouble();
             locationY = zip.nextDouble();
             setLocationX(locationX);
             setLocationY(locationY);
     
     
             moveForward(direction);
             turnLeft(direction);
     
             System.out.println(report(direction));
             System.out.println(direction);
     
     
          }
       //method turnLeft changes the direction depending on the currentLocation
          static void turnLeft(String direction) { 
             String currentLocation = direction;
             if (currentLocation.equals ("North")) {
                setDirection( "West"); }
             else if (currentLocation.equals("West")) {
                setDirection( "South"); }
             else  if (currentLocation.equals("South")) {
                setDirection("East"); }
             else {
                setDirection( "North"); }
     
          }
       //method moveForward changes the location depending on the currentLocation
          static void moveForward(String direction2) { 
             String currentLocation = direction2;
             if (currentLocation.equals("South") ){
                setLocationY( getLocationY() + 1); }
             if (currentLocation.equals("West")) {
                setLocationX(getLocationX() - 1); }
     
     
          }
       //method report prints the following: 
          static  String report(String direction) {
             String str =     getName() + ", " + getLocationX() + ", " + getLocationY() + ", " + getDirection(); 
             return (str);
          }
     
          public static void setName(String name2)
          {
             name = name2;
          }
     
          public static String getName()
          {
             return name;
          }
     
          public static void setLocationX(double locationX2)
          {
             locationX = locationX2;
          }
     
          public static double getLocationX()
          {
             return locationX;
          }
     
          public static void setLocationY(double locationY2)
          {
             locationY = locationY2;
          }
     
          public static double getLocationY()
          {
             return locationY;
          }
     
          public static void setDirection(String direction2)
          {
             direction = direction2;
          }
     
          public static String getDirection()
          {
             return direction;
          }
       }

    Took me a while to figure out what to do.

    If I could have remembered sooner and not been kinda tired, I'd have suggested using setter and getter methods.
    Last edited by javapenguin; July 11th, 2011 at 08:23 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with getX() and getY()

    The first change you should make with your code is to remove all of the static modifiers but the one needed on the main method. You are giving poor advice with this code.

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trouble with getX() and getY()

    Quote Originally Posted by Norm View Post
    You are giving poor advice with this code.
    In addition to spoon-feeding an answer. Javapenguin, you've been warned about this in the past, and I highly suggest you read, study, and take the points seriously in the following link: http://www.javaprogrammingforums.com...n-feeding.html.

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with getX() and getY()

    Thanks for all the points. I am not going to use your code, but I understand where you were going with it!

    currentLocation starts off being the direction that's entered (North, south, east, west). Let's say the user creates a robot named Joe and enters the direction as South, and the location as 1, 3. If the user inputs the cmd j.turnLeft .. the program will take the direction South, and change the current location to east. then if the person says j.turnLeft again, the current location will be east, and the program will change that to north.

    Is setDirection and setLocation a command? We haven't learned that yet so I can't use it, but I am just curious.

  14. #14
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with getX() and getY()

    Also, another thing I tried was to make the location input a string. then I tried to parse it into an integer which didn't work, but I was wondering if I could parse just the integers from it and use the getX() and getY() procedures there. Since we haven't learned set, I am wondering how it's possible to get the x and y out of the input: x, y.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with getX() and getY()

    Is setDirection and setLocation a command?
    They look like the names of methods.
    I usually think of commands as the name of an executable program that you want the OS to execute.
    Hence the term: command line

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trouble with getX() and getY()

    how it's possible to get the x and y out of the input: x, y.
    The String class has a method that will split that String into two Strings: x and y in an array.
    Then you could use the Integer class to convert those two Strings into int values.

Similar Threads

  1. Trouble with my applet
    By cdeed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 29th, 2011, 12:56 PM
  2. Having trouble returning the right out put.
    By byako in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2011, 11:49 PM
  3. trouble with GUI
    By MrHardRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 04:00 PM
  4. [SOLVED] mouse getX and getY are off?
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 6th, 2010, 12:49 AM
  5. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM

Tags for this Thread