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 3 of 3

Thread: ArrayList Errors

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default ArrayList Errors

    // I was asked to create an arraylist of strings, print out each element in
    // the arraylist, the longest string and it's location. Below are the errors I am getting and the code.

    Errors :
    ArrayListString.java:27: cannot find symbol
    symbol  : variable length
    location: class java.util.ArrayList<java.lang.String>
    		for(int i = 0; i < arraylistStr.length; i++)
    		                               ^
    ArrayListString.java:29: cannot find symbol
    symbol  : variable arrayStr
    location: class ArrayListString
    			if(arrayStr[i] != null)   //int index = 0;
    			   ^
    ArrayListString.java:31: array required, but java.util.ArrayList<java.lang.String> found
    				if(arraylistStr[i].length() > largest)
    				               ^
    ArrayListString.java:33: array required, but java.util.ArrayList<java.lang.String> found
    					largest = arraylistStr[i].length();
    					                      ^
    ArrayListString.java:39: array required, but java.util.ArrayList<java.lang.String> found
    		System.out.println("Element " + location + ", " + arraylistStr[largest]
    		                                                              ^
    5 errors

    import java.util.ArrayList;
     
    public class ArrayListString  
    {
    	public static void main(String args[])
    	{
    		ArrayList<String> arraylistStr = new ArrayList<String>();
     
    		arraylistStr.add("Jim Bob");         // Add Strings to the first 3 elements
    		arraylistStr.add("Bobby Jones");
    		arraylistStr.add("Rob Styles");
     
    		for(int count = 0; count <= arraylistStr.size(); count++)   // Print each element in arraylist			
    		{
    		 	System.out.println(arraylistStr.get(count));
    		}
     
    		int largest = 0, location = 0;   // Start largest off at 0
     
    		for(int i = 0; i < arraylistStr.length; i++)
    		{	
    			if(arrayStr[i] != null)   //int index = 0;
    			{
    				if(arraylistStr[i].length() > largest)
    				{
    					largest = arraylistStr[i].length();
    					location = i;
    				}
    			}
    		}	
     
    		System.out.println("Element " + location + ", " + arraylistStr[largest]
    		+ ", is the largest and its size is " + largest);
    	}
    }
    Last edited by pbrockway2; December 23rd, 2012 at 02:05 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: ArrayList Errors

    Please use "code" tags when you post code. [code]your code here[/code]

    --- Update ---

    "Cannot find symbol" is the compiler's way of saying that you have used a variable or method but the compiler can't find it defined anywhere.

    Common causes are are typos where you mean one thing, but type another (leave letters out, get the case wrong etc). Basically, spelling matters and you will get a "Cannot find symbol" with every spelling mistake. With methods they have to have the same number and type of arguments that they are defined with. For example length() is a method that lists have which takes exactly zero arguments and that is how it has to be used. The following will all yield "Cannot find symbol": Length(), length, lenght(), length(3). So check every variable to see that you have declared it somewhere, and every method to see you have spelt it correctly and have used parentheses with the correct number of arguments.

    The general idea of the code looks OK. But I have no idea why you have a null check in there. What is it supposed to do?

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList Errors

    You have referred arrayStr[i] instead of arraylistStr[i]. So, compiler is saying spelling mistake.

    Second thing before using collections/array/anything, first go through the usage, its syntax.
    1. You cannot refer ArrayList's element as arraylistStr[i]. You must use get() to retrieve its element.
    2. For checking the size of ArrayList, arraylistStr.size() is used not arraylistStr[i].length()

    Quote Originally Posted by mwardjava92 View Post
    // I was asked to create an arraylist of strings, print out each element in
    // the arraylist, the longest string and it's location. Below are the errors I am getting and the code.

    Errors :
    ArrayListString.java:27: cannot find symbol
    symbol  : variable length
    location: class java.util.ArrayList<java.lang.String>
    		for(int i = 0; i < arraylistStr.length; i++)
    		                               ^
    ArrayListString.java:29: cannot find symbol
    symbol  : variable arrayStr
    location: class ArrayListString
    			if(arrayStr[i] != null)   //int index = 0;
    			   ^
    ArrayListString.java:31: array required, but java.util.ArrayList<java.lang.String> found
    				if(arraylistStr[i].length() > largest)
    				               ^
    ArrayListString.java:33: array required, but java.util.ArrayList<java.lang.String> found
    					largest = arraylistStr[i].length();
    					                      ^
    ArrayListString.java:39: array required, but java.util.ArrayList<java.lang.String> found
    		System.out.println("Element " + location + ", " + arraylistStr[largest]
    		                                                              ^
    5 errors

    import java.util.ArrayList;
     
    public class ArrayListString  
    {
    	public static void main(String args[])
    	{
    		ArrayList<String> arraylistStr = new ArrayList<String>();
     
    		arraylistStr.add("Jim Bob");         // Add Strings to the first 3 elements
    		arraylistStr.add("Bobby Jones");
    		arraylistStr.add("Rob Styles");
     
    		for(int count = 0; count <= arraylistStr.size(); count++)   // Print each element in arraylist			
    		{
    		 	System.out.println(arraylistStr.get(count));
    		}
     
    		int largest = 0, location = 0;   // Start largest off at 0
     
    		for(int i = 0; i < arraylistStr.length; i++)
    		{	
    			if(arrayStr[i] != null)   //int index = 0;
    			{
    				if(arraylistStr[i].length() > largest)
    				{
    					largest = arraylistStr[i].length();
    					location = i;
    				}
    			}
    		}	
     
    		System.out.println("Element " + location + ", " + arraylistStr[largest]
    		+ ", is the largest and its size is " + largest);
    	}
    }

Similar Threads

  1. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  2. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM