Results 1 to 15 of 15
Thread: Im clueless, please help
- 03-01-2011, 11:21 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
Im clueless, please help
I am trying to get my toString() method to print the variable 'value' but i get the error "non-static variable value cannot be referenced from a static context". The thing is that i need the variable value to be an instance variable and not static. Is the toString method somehow static or am i just missing something crucial here? It might be an idiot question but i am really lost. Any help will be appreciated.
Java Code:public String toString() { String line = ("|" + FutoshikiSquare.value + "|"); return line; }Thanks in advanceJava Code:public class FutoshikiSquare { public int value; public FutoshikiSquare(int val) { value = val; } }
- 03-01-2011, 11:24 PM #2
None of the code you posted is static so it is not causing the error. Copy and paste the exact error message you get as well as your code and indicate which line is causing the error.
- 03-01-2011, 11:29 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
is the toString method part of the class?
I am aware I basically gave the answer a bit too easily, but from the code showed it looks like you defined a toString method somewhere else then called it, or you didnt create an instance of the class before calling the method.Java Code:class MyClass{ int value; public String toString(){ return "" + value; } public static void main(String[] args){ MyClass mc = new MyClass() mc.value = 20; mc.toString(); } }
- 03-01-2011, 11:32 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
I have posted the exact error message.
And the line causing the error is this one
Im not sure if the rest of the code will help but here it is anywayJava Code:public String toString() { [COLOR="Orange"] String line = ("|" + FutoshikiSquare.value + "|");[/COLOR] return line; }
Java Code:public class Futoshiki { /**************************************************************** * constant to specify size of puzzle (default 5) ****************************************************************/ public static final int GRIDSIZE = 5; private FutoshikiSquare[][] squares; private String[][] rowConstraints; private String[][] columnConstraints; private String temp; private String allErrors; /**************************************************************** * Creates a completely blank puzzle ****************************************************************/ public Futoshiki() { squares = new FutoshikiSquare[GRIDSIZE][GRIDSIZE]; rowConstraints = new String[GRIDSIZE][GRIDSIZE - 1]; columnConstraints = new String[GRIDSIZE][GRIDSIZE - 1]; allErrors = " ERRORS: "; //set up initial row constraints (no constraints) for (int row = 0; row < GRIDSIZE; row++) { for (int column = 0; column < GRIDSIZE - 1; column++) { rowConstraints[row][column] = " "; } } //set up initial column constraints (no constraints) for (int column = 0; column < GRIDSIZE; column++) { for (int row = 0; row < GRIDSIZE - 1; row++) { columnConstraints[column][row] = " "; } } } /************************************************************* * sets cell at specified row and column to the value specified * @param row the row number * @param column the column number * @param val the value to be stored *************************************************************/ public void setSquare(int row, int column, int val) { if ((val != 0) && (val <= GRIDSIZE)) { squares[row][column] = new FutoshikiSquare(val); } else { System.out.println("Illegal value - " + val); } } /*************************************************************** * Used to set up the puzzle in a legal starting state ***************************************************************/ public void fillPuzzle() { //sets up a Futoshiki puzzle setColumnConstraint(0, 1, ">"); setRowConstraint(4, 0, "<"); setRowConstraint(4, 2, "<"); setColumnConstraint(4, 3, "<"); setColumnConstraint(4, 2, "<"); setRowConstraint(3, 1, "<"); setRowConstraint(1, 3, ">"); setSquare(4, 0, 4); } /************************************************************* * sets a constraint in a specified row between the cell at the * specified column and the next one * @param row the row number * @param col the column number * @param relation the constraint (">" or "<") *************************************************************/ public void setRowConstraint(int row, int col, String relation) { if (relation.equals("<") || relation.equals(">")) { rowConstraints[row][col] = relation; } } /************************************************************* * sets a constraint in a specified column between the cell at the * specified row and the next one * @param col the column number * @param row the row number * @param relation the constraint ("<" or ">") *************************************************************/ public void setColumnConstraint(int col, int row, String relation) { if (relation.equals("<") || relation.equals(">")) { columnConstraints[col][row] = relation; } } public String toString() { String line = ("|" + FutoshikiSquare.value + "|"); return line; } /******************************************************************** * displays the puzzle in Ascii format on the console window ********************************************************************/ public void printPuzzle() { for (int row = 0; row < GRIDSIZE - 1; row++) { drawRow(row); drawColumnConstraints(row); } drawRow(GRIDSIZE - 1); } private void printTopBottom() { for (int col = 0; col < GRIDSIZE; col++) { System.out.print("--- "); } System.out.println(); } private void drawColumnConstraints(int row) { for (int col = 0; col < GRIDSIZE; col++) { String symbol = " "; if (columnConstraints[col][row].equals("<")) { symbol = "^"; } else if (columnConstraints[col][row].equals(">")) { symbol = "V"; } System.out.print(" " + symbol + " "); } System.out.println(); } private void drawRow(int row) { printTopBottom(); for (int col = 0; col < GRIDSIZE; col++) { String symbol; if (squares[row][col] != null) { System.out.print(toString()); } else { System.out.print("| |"); } if (col < GRIDSIZE - 1) { System.out.print(" " + rowConstraints[row][col] + " "); } } System.out.println(); printTopBottom(); }
- 03-01-2011, 11:35 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Try using this, it should work
public class FutoshikiSquare
{
public int value;
public FutoshikiSquare(int val)
{
value = val;
}
public String toString()
{
String line = ("|" + this.value + "|");
return line;
}
}
- 03-01-2011, 11:37 PM #6
No you didn't. This would be an exact error message.
FutoshikiSquare is the class and value is an instance variable of the class. You are trying to access it statically. That is classname.variablename, instead of through an instance, instanceOfClass.variablename.Java Code:FileTest.java:16: non-static variable x cannot be referenced from a static conte xt System.out.println(x); ^ 1 error
- 03-01-2011, 11:56 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Copy and past the compiler error in a code block.
Also, you can make toString much simpler
is all you need.Java Code:public String toString(){ return "|" + value + "|"; }
- 03-02-2011, 12:08 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
ok, i see where my mistake is. But the thing that pushed me in the wrong direction is that when i create the instance of the class i have to specify the int parameter of the constructor right?\
But the int number is actually specified by the setSquare method, so if i put in a number it automatically overrides the original one. How can i avoid this? :SJava Code:public String toString() { FutoshikiSquare f =[COLOR="SandyBrown"] new FutoshikiSquare(3)[/COLOR]; String line = ("|" + f.value + "|"); return line; }
Again, I'm sorry if i sound confusing but im new to java and i am rather confused as well. i hope you can tolerate me a bit longer xDJava Code:public void setSquare(int row, int column, int val) { if ((val != 0) && (val <= GRIDSIZE)) { squares[row][column] = new FutoshikiSquare(val); } else { System.out.println("Illegal value - " + val); } }
- 03-02-2011, 12:13 AM #9
If the toString method is in the FutoshikiSquare class then follow the advice given by sunde. I had to make assumptions since the information you provided was incomplete.
- 03-02-2011, 12:21 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
The toString method is in the Futoshiki class. FutoshikiSquare is a separate class holding only the value of a particular square
- 03-02-2011, 12:23 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, if toString is inside the class do not create an instance of it.
toString() acts as if it is applied to an item
for example, if you create a class with a toString method
it doesn't need to be told what it is working on, since its in the class it expects to be called like this in mainJava Code:public class MyClass{ int x; MyClass(int x){ this.x = x; } public String toString(){ return "|" + x + "|"; } }
In main you created the object and you called toString on the object. it uses the object to get the information(in this case mc) whatever the value of mc.x, it is used in toStrings return statement.Java Code:public static void main(String[] args){ MyClass mc = new MyClass(10); mc.toString(); }
I re read your code a little bit, and I am assuming you left out the FSquare class code, try defining the toString method in that class. Then the toString method in your main F class can loop through the multi-dimensional array of FSquare calling it's toString and adding it to F's String.
I believe this is what your ultimate question is, however I could be wrong.Java Code:class MyClassSquare{ MyClass[][] squares; public String toString(){ StringBuilder sb = new StringBuilder(""); for(int i = 0; i < squares.length; i++){ for(int j = 0; j < squares[i].length; j++){ sb.append(squares[i][j].toString()); } sb.append("\n"); } String s = sb.toString(); return s; }Last edited by sunde887; 03-02-2011 at 12:36 AM.
- 03-02-2011, 12:31 AM #12
I think you are going about this the wrong way. The FutoshikiSquare class should have a toString method. Then in the Futoshiki class (which has a 2D array of FutoshikiSquare objects) iterate over the 2D array and call the toString method of each FutoshikiSquare object.
- 03-02-2011, 12:42 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
- 03-02-2011, 12:52 AM #14
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
yes, this actually seems quite reasonable. I'll go ahead and try it. Thank you guys for the help, i really appreciate it :)
Also, do i preserve the right to keep this thread going if i get stuck again? :o
- 03-02-2011, 12:59 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ya that works if you expect to get stuck, but I believe it's generally accepted to mark this one solved with the thread tools and create a new one if you get stuck again. Your next problem could be something totally different and you may get more attention writing a new thread with a concise title.
I hope we helped you understand things a little more clearly.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks