Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Error:Cannot find Symbol

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Error:Cannot find Symbol

    I'm having some trouble. Right now it's just that the compilier is telling me it cannot find the symbol



    Grades.java:10: error: cannot find symbol
    ArrayList<Integer> Grades = new ArrayList<Integer>();
    ^
    symbol: class ArrayList
    location: class Grades
    [/I]
    public class Grades
    {
     
    public static void main(String []args)
    {
     
    int sum = 0;
    int Grades;
     
    ArrayList<Integer> Grades = new ArrayList<Integer>();
    Grades.add(100);
    Grades.add(90);
    Grades.add(80);
    Grades.add(70);
     
    for(integer element : values){
     
    sum = sum + element;
     
    }
     
    for (int i = 0; i <Gradebook.size(); i++)
    {
     if (i > 0)
     {
      System.out.printl(Gradebook.get(i));
      }


    --- Update ---

    I have never gotten this error before and I copied the ArrayList<> Grades = new ArrayList<>(); from the book so I am clueless as to why I am getting an error


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error:Cannot find Symbol

    The ArrayList class is in a package. You need an import statement that tells the compiler the name of that package.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Error:Cannot find Symbol

    Don't forget to import java.util.ArrayList.

  4. #4
    Member
    Join Date
    Dec 2012
    Posts
    31
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Error:Cannot find Symbol

    Nvm fixed that one too

    --- Update ---

    Alright my code compiled without a problem. Thank you guys

    --- Update ---

    //This class will take four grades then display them, remove the first element and display the new Array and Display the Array size and the second object.
     
     
    import java.util.ArrayList; //Imports Arraylist
     
    public class Gradebook     // Main class
    {
     
    public static void main(String []args) //Main method
    {
     
    ArrayList<Integer> Grades = new ArrayList<Integer>(); //creates a new Array;ist
    Grades.add(100);   //Adds 100 to element 0
    Grades.add(90);    // Adds 90 to element 1
    Grades.add(80);    // Adds 80  to element 2
    Grades.add(70);   //Adds 70 to element 3
     
    for(Integer element : Grades){     //The enchanced for loop statement
     
    }
     
    for (int i = 0; i <Grades.size(); i++) //Sets up a break between each number
    {
     if (i > 0)
     {
     System.out.print("|");   //The break to make it easier to read
     
     }
     
      System.out.println(Grades.get(i)); //Displays all the grades
      }
     
    Grades.remove(0); //Removes the first grade
     
    System.out.println(Grades); //Displays the new Grade
     
     
     
    int n = Grades.size();   //Puts the ArrayLists number of elements into int n
    System.out.printf("%d",n);//Displays int n, which is the number of elements
     
    System.out.println(Grades.get(1)); //Displays the second object
     
    }
     
    }


    --- Update ---

    Yeah new problem came up. When I want to access the second object in the Arraylist, which would be element 1, I am getting 380...Do you know why i am getting that?

    --- Update ---

    Am I getting the reference number maybe?

    --- Update ---

    Nvm. I was being stupid forgot that 3 and 80 weren't being split up.

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Error:Cannot find Symbol

    You will want to improve your code formatting using consistent indentation. Your for loops especially are very difficult to read and it's hard to know what code is inside and what is outside of the for loop. Please edit your code above as this will help both you and us.

    It sort of appears that you're removing an element from the ArrayList from within the for loop, and if so, don't do that as you'll get unexpected side effects. For one if you remove the 0th element while its looping, the next element you get an the next iteration of the loop won't be the next item in the ArrayList because the list has changed.

    Edit: no you're not removing items in the loop so ignore that, but again please please fix your indenting. It shows that you respect our efforts and free time if you put in efforts to make it easier for us to read your code.

Similar Threads

  1. [SOLVED] cannot find symbol error
    By Topflyt in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 5th, 2011, 08:57 AM
  2. Cannot find symbol ERROR
    By yacek in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 21st, 2011, 11:39 PM
  3. error :cannot find symbol
    By iswan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 1st, 2011, 08:26 AM
  4. Cannot find symbol error
    By AnuR in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 23rd, 2011, 02:50 PM
  5. cannot find symbol Error
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2010, 02:36 AM