Results 1 to 14 of 14
Thread: toString
- 05-02-2010, 04:08 AM #1
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
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 + ")";
}
}
-
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 04:20 AM.
- 05-02-2010, 04:25 AM #3
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
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 04:42 AM.
- 05-02-2010, 04:55 AM #5
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
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
-
- 05-02-2010, 05:04 AM #7
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
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() { ... }
}
-
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 }
- 05-02-2010, 05:31 AM #9
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
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 + ")";
}
}
-
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!
- 05-02-2010, 08:10 AM #11
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
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 + ")";
}
}
- 05-02-2010, 08:32 AM #12
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 + ")";
- 05-02-2010, 09:09 AM #13
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
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 aboveLast edited by justin23; 05-02-2010 at 09:13 AM.
-
???
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 + ")"; } }
Similar Threads
-
toString
By luckyleaf95 in forum New To JavaReplies: 9Last Post: 02-11-2010, 08:52 AM -
Formatting a toString
By MooNinja in forum New To JavaReplies: 8Last Post: 03-31-2009, 07:32 PM -
toString() method
By 01allenh in forum New To JavaReplies: 2Last Post: 03-25-2009, 11:43 PM -
toString question
By mayhewj7 in forum New To JavaReplies: 1Last Post: 01-29-2009, 07:41 PM -
Can i just use toString?
By cachi in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:32 PM
Bookmarks