Troubles with toString and static and non-static
Ok, in this assignment I have to write the code for a game that involves picking up and putting down stones in the right combination in order to unlock the treasure chest.
We've been advised that it only needs 4 classes i.e Player, Game, TreasureChest and Stones
My problem comes when I try to get the Game class to run a toString method that when it's finished should return something like this "Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)"
This is what my code looks like for the Game class so far:
Code Java:
import java.util.Scanner;
public class Game
{
private int maxMoves;
private int combination;
private String name;
private TreasureChest chest;
private Player player;
public Game()
{
Game game1 = new Game();
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter combination for the treasure chest (5-10): ");
int combination = keyboard.nextInt();
keyboard.nextLine();
chest = new TreasureChest(combination);
System.out.print("Enter maximum allowed moves: ");
int maxMoves = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter player name: ");
String name = keyboard.next();
keyboard.nextLine();
game1.toString();
}
public String toString()
{
return name + "";
}
}
I still need to add the other information that the toString method is supposed to return, but I can't get it to return even the name. When I try to initialise the Game class it is supposed to ask for the combination, max moves and player name. Instead I get the error "java.lang.StackOverflowError:null"
Any pointers?
Re: Troubles with toString and static and non-static
I'm not really sure what this has to do with toString or the static keyword.
In your Game constructor, you create a new Game, which calls the Game constructor, which creates a new Game, which...
Hint: Put a System.out.println("here"); or something as the first line in your Game constructor.
Re: Troubles with toString and static and non-static
Ok, that makes sense. If I remove that first line of code what do I need to change the "game1.toString(); line to for the method to actually run? That was where the static and non-static issues come from.
If I change it to Game.toString(); I get the error message "non-static method toString() cannot be referenced from a static context."
Re: Troubles with toString and static and non-static
Quote:
Originally Posted by
BadgerWatch
Ok, that makes sense. If I remove that first line of code what do I need to change the "game1.toString(); line to for the method to actually run? That was where the static and non-static issues come from.
If I change it to Game.toString(); I get the error message "non-static method toString() cannot be referenced from a static context."
When you're in the constructor, you're already "in" an instance of Game, so you don't have to refer to a game instance at all. You could use this.toString(), but the "this" part is implied, so simply calling toString() will do it. Calling Game.toString() is looking for a static method called toString, which you don't have.
But also, you're not doing anything with the String returned from the toString() method. I assume you want to print it out or something?
Re: Troubles with toString and static and non-static
Oh of course. Yeah eventually, once I get the rest of the code up to scratch it is supposed to print out "Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)" to show the player what the game situation is.
Thanks for the quick responses I'll get back into it and see how it goes ^_^
Re: Troubles with toString and static and non-static
Cool, glad you got it sorted.
Re: Troubles with toString and static and non-static
I'm back -_-
When I try to get the toString to return the "name" all it returns is null.
Code java:
import java.util.Scanner;
public class Game
{
private int maxMoves;
private int combination;
private String name;
private TreasureChest chest;
private Player player;
public Game()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter combination for the treasure chest (5-10): ");
int combination = keyboard.nextInt();
keyboard.nextLine();
chest = new TreasureChest(combination);
System.out.print("Enter maximum allowed moves: ");
int maxMoves = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter player name: ");
String name = keyboard.next();
keyboard.nextLine();
System.out.println(toString());
turn();
String move = keyboard.nextLine();
move.charAt(0);
}
public String toString()
{
return "" + name + "string1" + "string2" + "string3";
}
public void turn()
{
System.out.print("Move (l/r/p/d) : ");
}
}
Re: Troubles with toString and static and non-static
The reason is, you have a "name" variable already declared in the class private members.
You declare it again
Code :
String name = keyboard.next();
here.
So, your name is in this variable. And when toString() tries to get value, it goes to the private variable of the class which is null.
Note: Remove the String keyword from
Code :
String name = keyboard.next();
this statement and hopefully it will work all fine.