Results 1 to 14 of 14

Thread: toString

  1. #1
    justin23 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default toString

    I am very new to java and am having a few problams. i have the code below. I also have more code for the enemy and food classes. However i am am having some problems with the toString method, i need the toString method to return the coordinates that get entered for the player"you" in the form you(x,y) etc. i have had problem retrieving these x and y values and then outputing them i come up with numerous errors with everything i try

    public class Main
    {
    public static void main(String[] args)
    {
    Game game = new Game();

    }
    }
    import java.util.Scanner;
    import java.lang.Math.*;



    public class Game
    {

    private Scanner keyboard = new Scanner(System.in);

    private Main main;
    private Player player1;
    private Enemy enemy1, enemy2;
    private Food food1, food2;

    public Game()
    {

    {
    player1 = new Player();
    System.out.print("Enter your x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter your y coordinate: ");
    int y = keyboard.nextInt();
    player1.setPosition(x, y);
    }

    {
    food1 = new Food();
    System.out.print("Enter food's x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter food's y coordinate: ");
    int y = keyboard.nextInt();
    food1.setPosition(x, y);
    }
    {
    food2 = new Food();
    System.out.print("Enter food's x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter food's y coordinate: ");
    int y = keyboard.nextInt();
    food2.setPosition(x, y);
    }
    {
    enemy1 = new Enemy();
    System.out.print("Enter enemy's x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter enemy's y coordinate: ");
    int y = keyboard.nextInt();
    enemy1.setPosition(x, y);
    }
    {

    enemy2 = new Enemy();
    System.out.print("Enter enemy's x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter enemy's y coordinate: ");
    int y = keyboard.nextInt();
    enemy2.setPosition(x, y);
    }


    public String toString()
    {
    player1.getPosition();
    return "you(" + x + "," + y + ")";

    }
    }
    }

    import java.lang.Math.*;

    public class Player
    {
    private double position;

    public int x;
    public int y;


    public void setPosition(int newX, int newY)
    {
    x = newX;
    y = newY;
    }

    public double getNewX()
    {
    return x;
    }

    public double getNewY()
    {
    return y;
    }


    public String toString()
    {
    return "you(" + x + "," + y + ")";

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Cross-posted here: New To Java - To String

    To the original poster, welcome to the forum, and I hope your question gets answered, but I do request that if you cross-post a question you provide links to the other cross-posts. Nobody likes duplicating work that's already been done. Also it violates the agreement you signed on joining the forum.

    Have you cross-posted anywhere else? Please let us know.

    Thanks for your cooperation.
    Last edited by Fubarable; 05-02-2010 at 05:20 AM.

  3. #3
    justin23 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    Ok sorry, just need help quick and i thought the more places it gets posted the higher chance of a reply. Thats the only other place it has been posted.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by justin23 View Post
    Ok sorry, just need help quick and i thought the more places it gets posted the higher chance of a reply. Thats the only other place it has been posted.
    Thanks for the apology. Just realize that if you anger folks you'll get quite the opposite result intended: you actually decrease your chances of getting help which is the reason I warned you. I'm sure it won't happen again.

    Regarding your problem, what are your instructions for creating this Game class? You should understand that Game may have its own toString method, but it should not try to have it emulate Player's toString. Rather, it will likely call the toString methods of several of the objects that it contains.
    Last edited by Fubarable; 05-02-2010 at 05:42 AM.

  5. #5
    justin23 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    As part of the requirements the toString needs to be in the game class . The toString method in the Player class is just me trying anything to make it work really. it is my understanding the method string ToString() must be outlined however this may be incorrect

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by justin23 View Post
    As part of the requirements the toString needs to be in the game class . The toString method in the Player class is just me trying anything to make it work really. it is my understanding the method string ToString() must be outlined however this may be incorrect
    You might as well post the entire requirements.

  7. #7
    justin23 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    Implement the toString() method to produce and return a string of the form "You(0,2) Food(3,3) Food(5,3) Enemy(3,5) Enemy(5,5)" which displays the current position of each object, with the x-coordinate appearing first, and the y-coordinate appearing second.

    also it must have a Game class with the following constructors and methods:

    public class Game
    {
    public Game() { ... }

    public String toString() { ... }
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So I was right in post #4, you want to use the toString methods of the objects that Game contains in order to create a String for Game's toString to return. Something on this order:

    Java Code:
    public String toString() {
      return player1.toString() + ", " + food1.toString() + ", " + // you can figure out the rest
    }

  9. #9
    justin23 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    ok thanks now im having trouble with illegal start of expression on the line
    public String toString()

    import java.util.Scanner;
    import java.lang.Math.*;



    public class Game
    {

    private Scanner keyboard = new Scanner(System.in);

    private Main main;
    private Player player1;
    private Enemy enemy1, enemy2;
    private Food food1, food2;

    public Game()
    {

    {
    player1 = new Player();
    System.out.print("Enter your x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter your y coordinate: ");
    int y = keyboard.nextInt();
    player1.setPosition(x, y);
    }

    {
    food1 = new Food();
    System.out.print("Enter food's x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter food's y coordinate: ");
    int y = keyboard.nextInt();
    food1.setPosition(x, y);
    }
    {
    food2 = new Food();
    System.out.print("Enter food's x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter food's y coordinate: ");
    int y = keyboard.nextInt();
    food2.setPosition(x, y);
    }
    {
    enemy1 = new Enemy();
    System.out.print("Enter enemy's x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter enemy's y coordinate: ");
    int y = keyboard.nextInt();
    enemy1.setPosition(x, y);
    }
    {

    enemy2 = new Enemy();
    System.out.print("Enter enemy's x coordinate: ");
    int x = keyboard.nextInt();
    System.out.print("Enter enemy's y coordinate: ");
    int y = keyboard.nextInt();
    enemy2.setPosition(x, y);
    }


    public String toString()
    {
    return player1.toString() "You(" + x + "," + y + ")"
    food1.toString() "Food(" + x + "," + y + ")"
    food2.toString() "Food(" + x + "," + y + ")"
    enemy1.toString() "Enemy(" + x + "," + y + ")"
    enemy2.toString() "Enemy(" + x + "," + y + ")";

    }

    }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may be trying to declare this method from within the Game constructor and you can't do that. By the way, what's with all the extra curly braces in your constructor? All they do is cause confusion -- I'd get rid of them. Also, when posting code here, or in any forum, you'll want to format it better so more folks will be willing to read your code and your posts. Much luck!

  11. #11
    justin23 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    ok fixed that problem it now wants a semicolon after each line when there are already semicolons. Is this a common error?

    public String toString()
    {
    return player1.toString() "You(" + x + "," + y + ")" ;
    food1.toString() "Food(" + x + "," + y + ")" ;
    food2.toString() "Food(" + x + "," + y + ")" ;
    enemy1.toString() "Enemy(" + x + "," + y + ")" ;
    enemy2.toString() "Enemy(" + x + "," + y + ")";

    }

    }

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by justin23 View Post
    Is this a common error?

    yes, but if you work within an ide like eclipse the ide will show you such errors, so that you can't compile the code before you haven't fixed your bugs.

    i can't figure out what the purpose of this lines are? tell my ... and if put put your return before these lines the lines above will be unreachable.

    Java Code:
    food1.toString() "Food(" + x + "," + y + ")" ;
    food2.toString() "Food(" + x + "," + y + ")" ;
    enemy1.toString() "Enemy(" + x + "," + y + ")" ;
    enemy2.toString() "Enemy(" + x + "," + y + ")";

  13. #13
    justin23 is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    im trying to get the output

    Enter your x coordinate: 0
    Enter your y coordinate: 0
    Enter food's x coordinate: 3
    Enter food's y coordinate: 3
    Enter food's x coordinate: 5
    Enter food's y coordinate: 3
    Enter enemy's x coordinate: 3
    Enter enemy's y coordinate: 5
    Enter enemy's x coordinate: 5
    Enter enemy's y coordinate: 5
    You(0,2) Food(3,3) Food(5,3) Enemy(3,5) Enemy(5,5)



    i take it from your reply those 4 line should be with the line above
    Last edited by justin23; 05-02-2010 at 10:13 AM.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    ???
    Java Code:
    public String toString()
    {
    return player1.toString() "You(" + x + "," + y + ")" ;
    food1.toString() "Food(" + x + "," + y + ")" ;
    food2.toString() "Food(" + x + "," + y + ")" ;
    enemy1.toString() "Enemy(" + x + "," + y + ")" ;
    enemy2.toString() "Enemy(" + x + "," + y + ")";
    
    }
    
    }
    Why are you doing this? I already gave you the structure your toString should look like. All you have to do is finish that line in a similar way that I started it.

Similar Threads

  1. toString
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-11-2010, 09:52 AM
  2. Formatting a toString
    By MooNinja in forum New To Java
    Replies: 8
    Last Post: 03-31-2009, 08:32 PM
  3. toString() method
    By 01allenh in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 12:43 AM
  4. toString question
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 08:41 PM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •