Results 1 to 15 of 15
  1. #1
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default 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;
     
            }
    Java Code:
    public class FutoshikiSquare 
        {
           
            public int value;
         
    
            public FutoshikiSquare(int val)
            {
                value = val;
               
                
            }
            
            
        }
    Thanks in advance

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    is the toString method part of the class?
    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();
      }
    }
    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.

  4. #4
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    I have posted the exact error message.
    And the line causing the error is this one
    Java Code:
    public String toString()
            {
    
              [COLOR="Orange"] String line = ("|" + FutoshikiSquare.value + "|");[/COLOR]
               return line;
     
            }
    Im not sure if the rest of the code will help but here it is anyway

    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();
        }

  5. #5
    Aschee is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    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;

    }


    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by truant420 View Post
    I have posted the exact error message.
    No you didn't. This would be an exact error message.
    Java Code:
    FileTest.java:16: non-static variable x cannot be referenced from a static conte
    xt
            System.out.println(x);
                               ^
    1 error
    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.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Copy and past the compiler error in a code block.

    Also, you can make toString much simpler
    Java Code:
    public String toString(){
      return "|" + value + "|";
    }
    is all you need.

  8. #8
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    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?\

    Java Code:
    public String toString()
            {
               FutoshikiSquare f =[COLOR="SandyBrown"] new FutoshikiSquare(3)[/COLOR];
               String line = ("|" + f.value + "|");
               return line;
     
            }
    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? :S

    Java 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);
            }
        }
    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 xD

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  10. #10
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    The toString method is in the Futoshiki class. FutoshikiSquare is a separate class holding only the value of a particular square

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    public class MyClass{
     int x;
     MyClass(int x){
      this.x = x;
     }
     public String toString(){
      return "|" + x + "|";
     }
    }
    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 main
    Java Code:
    public static void main(String[] args){
      MyClass mc = new MyClass(10);
      mc.toString();
    }
    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.

    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.

    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;
    }
    I believe this is what your ultimate question is, however I could be wrong.
    Last edited by sunde887; 03-02-2011 at 01:36 AM.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Junky View Post
    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.
    THIS ^

    My longwinded explanation boils down to this.

  14. #14
    truant420 is offline Member
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    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

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

Posting Permissions

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