Results 1 to 7 of 7
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Parameter help/question

    Hi, I have a quick question and need help with a certain issue with parameters..

    Now I still consider myself learning and getting used to programming, and I don't quite understand the great use/function of parameters.. and when I use it in this program I'm working on, I get a certain error. I'm following a few lines of code from this website and it shows this:

    Java Code:
    public void setX(int x)
      {
        this.xPos = x;
      }
      
      public void setY(int y)
      {
        this.yPos = y;
      }
      
      public void draw (Graphics2D g2)
      {
        
        if (useImage == false)
        {
          g2.setColor(c);
          g2.fill(new Ellipse2D.Double(xPos, yPos, ballWidth, ballHeight));
        }
        else
        {
          g2.drawImage(getImage(), x, y, null);
        }
        
      }
      
      public void move()
      {
        this.x += 1;
        this.y += 2;
      }
      
      public void move(int xDir, int yDir)
      {
        this.x += xDir;
        this.y += yDir;
      }
    I'm only cutting out a bit of the whole program because this is the only issue I'm having. Two void methods I'm using is setX(int x) and setY(int y). But when I try to use them in other methods, I get an error saying "x cannot be resolved as a variable" and "y cannot be resolved as a variable". Is there any reason why it's saying this? Because the website I'm using wrote it exactly like that and it works for them, apparently. I'd also like to know a solution to this problem without changing the variables for the 3 methods after SetX/setY. One solution I had was changing the variables for those three methods from x/y to xPos/yPos (field variable used in the program) which generally works with the program, but as I said, I'm trying to learn how to use parameters outside of their methods so I want to find a solution sticking with x and y.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Parameter help/question

    when I try to use them in other methods, I get an error
    How are you compiling the program?
    Please post the full text of the error message. You edited version is leaving out information.
    Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Parameter help/question

    The only peculiarity I see is in line #21: where do the variables x and y come from?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Parameter help/question

    Quote Originally Posted by Norm View Post
    How are you compiling the program?
    Please post the full text of the error message. You edited version is leaving out information.
    Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    I simply press the Compile button at the top.. I'm getting 6 errors, all of them that say the same thing. "x cannot be resolved as a variable" and "y cannot be resolved as a variable" on lines 21, 28, 29, 34, and 35.

    Quote Originally Posted by JosAH View Post
    The only peculiarity I see is in line #21: where do the variables x and y come from?

    kind regards,

    Jos
    The variables x and y come from lines 1 and 8. They're from parameters from those two methods.

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

    Default Re: Parameter help/question

    Quote Originally Posted by CuppaCoffee View Post
    The variables x and y come from lines 1 and 8. They're from parameters from those two methods.
    Correct, and since they are parameters, they are only visible within the method that holds them and no where else. Do you have a similar variable in the class that has a different name, one that holds the values from the two methods that you mention above?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,393
    Rep Power
    25

    Default Re: Parameter help/question

    You need to look at the concept of "scope" for the definitions of variables. The definition of a variable in a program is limited to be within the enclosing {}s. Outside of the enclosing {}s the variable is NOT defined.
    The variable defined on line 1 is NOT defined past line 4. It is out of scope hence is not known, on line 21

  7. #7
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Parameter help/question

    I was thinking that too, I just wanted to make sure there was absolutely no way to use those variables outside of their methods. Alright then, now that I know it's true, it must be a mistake in the code. I'll simply go back to replacing those variables with xPos and yPos. Thanks for the help.

Similar Threads

  1. Ireport x parameter SQL
    By Reginaldo in forum Advanced Java
    Replies: 0
    Last Post: 10-21-2011, 02:03 PM
  2. how to pass parameter from one jsp to another jsp
    By kader_sit in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-10-2010, 02:04 PM
  3. Using ArrayList as a parameter
    By Crypts in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 06:56 AM
  4. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 09:26 PM
  5. Retrieving a parameter through URL.
    By hisouka in forum Java Servlet
    Replies: 1
    Last Post: 09-06-2008, 12:45 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
  •