Results 1 to 7 of 7
  1. #1
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default Need help calling from a different method

    So basically I'm trying to call the squares array from the method called Board and I want to use it in a method called pairInput. Both methods are within the same class called Board. Here the codes for both methods.

    Java Code:
        public Board()
        {
            int initialXValue = 25; // starting left-hand x value
            int x = initialXValue;  // x position
            int xIncrement = 70;    // increment to x for each column
            int initialYValue = 50; // starting left-hand x value
            int y = initialYValue;  // y position
            int yIncrement = 70;    // increment to y for each row
            
            // variables used for drawing
            
            Square gameTitle = new Square( 125, 20, 10, "white", true, "9 Tiles Puzzle");
            Square background = new Square( x-OFFSET, y-OFFSET, (SQUARE_SIZE+OFFSET)*(NUMBER_OF_ROWS) + OFFSET, "black", true, "");
            
            int[] presetArray = {3,2,7,5,8,4,6,1};
            Square squares[] = new Square[ 8];
            
            // Create 9 boxes at the correct spacing intervals for the background
            for(int i=0; i<8; i++){
            	int xtemp;
            	int ytemp;
            	if(i < 3){
            		xtemp = i*xIncrement + initialXValue;
            		ytemp = initialYValue;
            	}
            	else if(i < 6){
            		xtemp = (i-3)*xIncrement + initialXValue;
            		ytemp = initialYValue + yIncrement*1;
            	}
            	else{
            		xtemp = (i-6)*xIncrement + initialXValue;
            		ytemp = initialYValue + yIncrement*2;
            	}
            	squares[ i] = new Square(xtemp, ytemp, SQUARE_SIZE, "lightGray", true);
            }
            
            // Assign labels in a specific order to the square object array we just created
            for(int i=0; i<8; i++){
            	squares[ i].setLabel(Integer.toString(presetArray[ i]));
            }
            
        }  //end constructor Board()
    Java Code:
    public void pairInput( int p)
        {
            Square s1 = squares[ 0].getLabel();
            Square s2 = squares[ 1].getLabel();
            Square s3 = squares[ 2].getLabel();
            Square s4 = squares[ 3].getLabel();
            Square s5 = squares[ 4].getLabel();
            Square s6 = squares[ 5].getLabel();
            Square s7 = squares[ 6].getLabel();
            Square s8 = squares[ 7].getLabel();
            
            if( p == s1) {
                p = s1;
                px = p.getX();
                py = p.getY();
            }
            else if( p == s2) {
                p = s2;
            }
                else if( p == s3) {
                    p = s3;
                }
                    else if( p == s4) {
                        p = s4;
                    }
                        else if( p == s5) {
                            p = s5;
                        }
                            else if( p == s6) {
                                p = s6;
                            }
                                else if( p == s7) {
                                    p = s7;
                                }
                                    else if( p == s8) {
                                        p = s8;
                                    }
                                    
        }

    Also if you see the part where it says px=p.getX(); and the same for y, I am trying to call a method called getX() from a class called Square. Not sure if I wrote that correctly or not either...

    Any help would be greatly appreciated.

    -Mayur

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

    Default

    public Board() is not a method; it's a constructor. You appear to be declaring your Square[] array from within the constructor where it will be invisible to the rest of the class. I have no idea what your error or problem is (you never give us a clue as to any error messages you may be getting), but you may wish to declare this array as a class array and not a local array within the constructor. You also may wish to tell us more about what is not working here. It's the weekend and I left my crystal ball at work, so I can't really guess. :rolleyes:

  3. #3
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    Ok I see what you are saying and I seemed to have fixed my errors...for now at least lol. Thanks for your help.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    just a tip, when writing your code, you should follow a more standard way on indenting. what you have is:

    Java Code:
    if()
       else if()
          else if()
             else if()
                else
    this will become confusing as you move on and find yourself nesting if-statements (putting an if-statement inside an if-statement). what's standard is:

    Java Code:
    if()
    else if()
    else if()
    else

  5. #5
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    Ok now I need help calling this method from a different class.
    Java Code:
                int px = Square.s1.getX();
    I am trying to call the method getX() from the Square class and currently it is telling me that cannnot find variable s1 but if you look at my pairInput method from above, s1 is there...so not sure.

    -Mayur

  6. #6
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    just a tip, when writing your code, you should follow a more standard way on indenting. what you have is:

    Java Code:
    if()
       else if()
          else if()
             else if()
                else
    this will become confusing as you move on and find yourself nesting if-statements (putting an if-statement inside an if-statement). what's standard is:

    Java Code:
    if()
    else if()
    else if()
    else
    My professor told us to write else if statments like that if they are like nested or something. That is what I did on my last program and I got a 100% on it so not sure. I too find it harder to read but that's how he wants it.

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

    Default

    without the "else" statements, he would be correct. With the else keywords, then indentation would be wrong, and he'd agree with this.
    am trying to call the method getX() from the Square class and currently it is telling me that cannnot find variable s1 but if you look at my pairInput method from above, s1 is there...so not sure.
    Ay yi yi, you need to refresh some basic Java here. s1 is not a field of the Square class. It is a variable local to the pairInput method.

    edit: you also appear to be using == to compare Square objects which won't work for objects. Instead you should use the equals(...) method.

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 05:01 PM
  2. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  3. Trouble will calling a method
    By jonsamwell in forum New To Java
    Replies: 9
    Last Post: 08-22-2008, 11:16 PM
  4. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 04:38 AM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 04:27 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
  •