Results 1 to 4 of 4
  1. #1
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Why Are these Variables Not working as they should?

    In the sortStats method, I have highestItemPriority and averagePriority, as you can see.

    Are these variables not able to be used in the following displayResults() method?


    public void sortStats()
    {
    double nextItemPriority = list[0].getItemPriority();
    double highestItemPriority = nextItemPriority;
    double sum = nextItemPriority;
    for (int i = 1; i <= numberOfItems; i++)
    {
    nextItemPriority = list[i].getItemPriority();
    if (nextItemPriority > highestItemPriority)
    highestItemPriority = nextItemPriority;
    }
    double averagePriority = sum / numberOfItems;
    }

    public void displayResults()
    {
    System.out.println("Highest Priority Item is: ");
    for (int i = 1; i <= numberOfItems; i++)
    {
    double nextItemPriority = list[i].getItemPriority();
    if (nextItemPriority == highestItemPriority)
    {
    list[i].writeOutput();
    System.out.println
    ((nextItemPriority - averagePriority)
    + " above average");
    System.out.println();
    }
    }

  2. #2
    jing-yi is offline Member
    Join Date
    May 2011
    Posts
    35
    Rep Power
    0

    Default Re: Why Are these Variables Not working as they should?

    I guess you copy the code from somewhere.

    What you need to do is do a declaration on highestItemPriority and averagePriority variable.
    Then this will solve the issue.

  3. #3
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Why Are these Variables Not working as they should?

    They are declared, I did not copy this copy from somewhere else.

    I will keep looking at this and see if I find something else, I'm sure it's obvious.

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

    Default Re: Why Are these Variables Not working as they should?

    Quote Originally Posted by jakaldama View Post
    They are declared, I did not copy this copy from somewhere else.

    I will keep looking at this and see if I find something else, I'm sure it's obvious.
    No they are not; those variables are declared as locals in one method so they can't be used in another method; your compiler already told you so.,

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Collisions are working & not working
    By Jayayoh in forum Java 2D
    Replies: 1
    Last Post: 06-24-2011, 05:21 PM
  2. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 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
  •