Results 1 to 7 of 7
  1. #1
    johnb082 is offline Member
    Join Date
    Jun 2011
    Posts
    16
    Rep Power
    0

    Default USing JGrasp to Compile and receiving Cannot find symbol

    Hello,

    I am a student. I'm up against a wall and need assistance. Below is my code. When I try to compile, I receive one error. The error occurs on: items[i] = keyboard.nextLine(); and prices[i] = keyboard.nextDouble(); The i is defined in the for statement. I am unclear as to why. Please let me educate me in the err of my ways. Thank you.

    import java.util.*;

    public class MalottPricesArray {

    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    double[] prices = new double [5];
    String []items = new String [5];
    String input;
    int sum=0;
    input=keyboard.nextLine();

    //Loading the Array
    for (int i = 0; i < 5; i++){
    }

    {
    System.out.print("Enter produce of your choice " + i+1 + ": ");
    items[i] = keyboard.nextLine();
    System.out.print("Enter price for the produce # " + i+1 + ": ");
    prices[i] = keyboard.nextDouble();
    keyboard.nextLine();
    }
    //end Main

    //sum
    for(int i = 0; i < 5; i++){
    sum += prices[i];
    }
    {
    System.out.println("The sum of all the values: "+sum);
    }
    //end sumArray method

    //average
    double average=sum/5;
    {
    System.out.println("Average of all values: "+average);
    }
    //end aveArr method

    //highPrices
    System.out.println("The value that are larger than the average: ");
    for(int i=0;i<prices.length;i++){
    if(prices[i]>average){
    System.out.println(prices[i]);
    }//end if
    }//end for
    }//end Main
    }//end Class

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Can you post the full text of the error message? You don't say what symbol is not found.
    Java Code:
    System.out.print("Enter produce of your choice " + i+1 + ": ");
    This may not print as you suspect. Do you want to add 1 to i and print the total? Or print the value of i and the 1
    Then put the expression in ()s: (i+1)


    Check your {}s there are a few extra ones in your code.
    Last edited by Norm; 06-23-2011 at 10:46 PM.

  3. #3
    johnb082 is offline Member
    Join Date
    Jun 2011
    Posts
    16
    Rep Power
    0

    Default

    Here is what I receive from the compiler.

    MalottPricesArray.java:28: cannot find symbol
    symbol : variable i
    location: class MalottPricesArray
    System.out.print("Enter produce of your choice " + (i+1) + ": ");
    ^
    MalottPricesArray.java:29: cannot find symbol
    symbol : variable i
    location: class MalottPricesArray
    items[i] = keyboard.nextLine();
    ^
    MalottPricesArray.java:30: cannot find symbol
    symbol : variable i
    location: class MalottPricesArray
    System.out.print("Enter price for the produce # " + (i+1) + ": ");
    ^
    MalottPricesArray.java:31: cannot find symbol
    symbol : variable i
    location: class MalottPricesArray
    prices[i] = keyboard.nextDouble();
    ^
    4 errors

  4. #4
    johnb082 is offline Member
    Join Date
    Jun 2011
    Posts
    16
    Rep Power
    0

    Default

    I also want to let you know I added the () around i+1.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Check your {}s. The variable i is out of scope where you are trying to use it.

  6. #6
    johnb082 is offline Member
    Join Date
    Jun 2011
    Posts
    16
    Rep Power
    0

    Default

    I will do that. Just one question. I have viewing example reports when placing brackets {}. What is the rule of thumb for them and their placement?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    I use them when I need to have more than one statement grouped together as one unit.
    Mostly with if() {}, while() {}, for(){}

Similar Threads

  1. Constructor: Cannot find symbol compile error.
    By CaptainBeer in forum New To Java
    Replies: 4
    Last Post: 04-28-2011, 12:25 PM
  2. java file reader, jgrasp can't find the file
    By aramiky818 in forum New To Java
    Replies: 3
    Last Post: 04-22-2011, 03:06 AM
  3. Replies: 9
    Last Post: 10-18-2008, 09:26 PM
  4. Compile: symbol not found
    By zuan in forum New To Java
    Replies: 7
    Last Post: 06-24-2008, 06:17 AM
  5. Replies: 3
    Last Post: 05-01-2008, 09:30 AM

Posting Permissions

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