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

Thread: Cannot find symbol ERROR

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Cannot find symbol ERROR

    Working on a bin sort; user enters a bunch of number and then I sort them.

    I keep getting the error "cannot find symbol" and an arrow pointing to the period in mbin.elementAt(i) and ever time I have an arrayList command in my method. What's going on, can the method not access the arrayList created in the main? Help.

    //CS 108
     
    import java.util.*;
    public class HW4
    {
     
    public static ArrayList<String> mbin = new ArrayList<String>();
    public static ArrayList[] sbins = new ArrayList[10];
     
    public static void main(String args[])
    {
     
        for (int i=0; i<=9; i++)     //create each of the sort bins, 0 through 9
        {
        sbins[i] = new ArrayList();
        }
     
    	Scanner input = new Scanner(System.in);
    	String numIn = null;
     
    	System.out.println("Input a value from 0 to 9999. When done inputting hit the ENTER key alone.");
     
    	while((numIn = input.nextLine( )).length( ) > 0)
    	{
    		if(numIn.length( ) > 4)
    		{
    		System.out.println("The number you entered is too big. Input a value from 0 to 9999.");
    		}
    		else
    		{
    		System.out.println("Input a value from 0 to 9999 if done inputting hit the ENTER key alone.");
    		mbin.add(numIn);
    		}
    	}
     
    	System.out.print("You entered: ");
    	System.out.println( mbin.toString() );
     
    	MtoS(3);
    	StoM();
    	MtoS(2);
    	StoM();
    	MtoS(1);
    	StoM();
    	MtoS(0);
    	StoM();
     
    	System.out.println( mbin.toString() );
    }
     
    public static void MtoS(int p)
    {
    	for(int i = 0; i < (mbin.size() - 1); i++)
    	{
    		String s = mbin.elementAt(i);
    		char c = ' ';
     
    		if(s.length( ) == 4)
    		{
    		c = s.charAt(p);
    		}
    		else if(s.length( ) == 3)
    		{
    		c = s.charAt(p-1);
    		}
    		else if(s.length( ) == 2)
    		{
    		c = s.charAt(p-2);
    		}
    		else
    		{
    		c = s.charAt(p-3);
    		}
     
     
    		if(c == '9')
    		{
    		sbins[9].addElement(s);
    		}
    		else if(c == '8')
    		{
    		sbins[8].addElement(s);
    		}
    		else if(c == '7')
    		{
    		sbins[7].addElement(s);
    		}
    		else if(c == '6')
    		{
    		sbins[6].addElement(s);
    		}
    		else if(c == '5')
    		{
    		sbins[5].addElement(s);
    		}
    		else if(c == '4')
    		{
    		sbins[4].addElement(s);
    		}
    		else if(c == '3')
    		{
    		sbins[3].addElement(s);
    		}
    		else if(c == '2')
    		{
    		sbins[2].addElement(s);
    		}
    		else if(c == '1')
    		{
    		sbins[1].addElement(s);
    		}
    		else
    		{
    		sbins[0].addElement(s);
    		}
    	}
    }
     
    public static void StoM()
    {
    	for(int i = 0; i < (sbins[9].size() - 1); i++)
    	{
    	String s = sbins[9].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[8].size() - 1); i++)
    	{
    	String s = sbins[8].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[7].size() - 1); i++)
    	{
    	String s = sbins[7].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[6].size() - 1); i++)
    	{
    	String s = sbins[6].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[5].size() - 1); i++)
    	{
    	String s = sbins[5].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[4].size() - 1); i++)
    	{
    	String s = sbins[4].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[3].size() - 1); i++)
    	{
    	String s = sbins[3].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[2].size() - 1); i++)
    	{
    	String s = sbins[2].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[1].size() - 1); i++)
    	{
    	String s = sbins[1].elementAt(i);
    	mbin.addElement(s);
    	}
    	for(int i = 0; i < (sbins[0].size() - 1); i++)
    	{
    	String s = sbins[0].elementAt(i);
    	mbin.addElement(s);
    	}
    }
     
     
    }


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Cannot find symbol ERROR

    Ok, so you should not be using an ArrayList like an array, it is a wrapper class, and it also should be specifying the type.

    ArrayList API

    Example of how to use ArrayList:
    ArrayList<Integer> anArrayOfInts = new ArrayList<Integer>();
    anArrayOfInts.add(5);
    anArrayOfInts.add(3);
    anArrayOfInts.add(7);
    System.out.println(anArrayOfInts.get(0)); //Aquires and prints the 0th value. (5)

    Also, if you are interested in help on sorting, you can find some here.
    Last edited by Tjstretch; October 21st, 2011 at 10:34 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol ERROR

    Where am I using arraylist like an array? Also, the error is only occurring in my methods.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Cannot find symbol ERROR

    public static ArrayList[] sbins = new ArrayList[10];
    sbins[i] = new ArrayList();

    While I don't see any reason why it would be illegal to have an array of ArrayLists, never tried it to be honest, I think the ArrayList in question has to have a type.

    I think if you had, for instance,

    public static ArrayList<String>[] sbins = new ArrayList<String>[10];

    it might work.

    Also, elementAt(int index) isn't a method.

    get(int index) which I think does what you're trying to do with "elementAt()" is a method.

    addElement(E element) doesn't work either.

    It's add(E element).

    Where E is the data type.
    Last edited by javapenguin; October 21st, 2011 at 10:47 PM.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol ERROR

    @Javapenguin You can have an array of ArrayLists. Tried adding the string generics to it but that doesn't change anything.

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Cannot find symbol ERROR

    Sorry, I assumed you were when I saw
    public static ArrayList[] sbins = new ArrayList[10];

    Please highlight your code with
    [highlight=Java] *Code* [/highlight]
    rather than code tags, although code is better than nothing

    The reason you are having trouble accessing the variable is because your methods are static, they only use the first variable, to solve this make it non-static (remove the static modifier) and don't call it from the main method. EG
    public class someClass
    {
      public void someNonStaticMethod()
      {
        //Code eluded
      }
      public someClass()
      {
        //Code here instead of main.
        someNonStaticMethod();
      }
      public static void main(String[] args)
      {
        new someClass();
      }
    }

  7. The Following User Says Thank You to Tjstretch For This Useful Post:

    javapenguin (October 21st, 2011)

  8. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol ERROR

    I can't figure it out, here's a totally simplified example I tried to compile. Same error occurs at the line where I have String s = mbin.elementAt(i);

    It's says "cannot find symbol", there are no methods at all now.

    //CS 108
     
    import java.util.*;
    public class HW4b
    {
     
    public static ArrayList<String> mbin = new ArrayList<String>();
    public static ArrayList[] sbins = new ArrayList[10];
     
    public static void main(String args[])
    {
     
        for (int i=0; i<=9; i++)     //create each of the sort bins, 0 through 9
        {
        sbins[i] = new ArrayList();
        }
     
    	Scanner input = new Scanner(System.in);
    	String numIn = null;
     
    	System.out.println("Input a value from 0 to 9999. When done inputting hit the ENTER key alone.");
     
    	while((numIn = input.nextLine( )).length( ) > 0)
    	{
    		if(numIn.length( ) > 4)
    		{
    		System.out.println("The number you entered is too big. Input a value from 0 to 9999.");
    		}
    		else
    		{
    		System.out.println("Input a value from 0 to 9999 if done inputting hit the ENTER key alone.");
    		mbin.add(numIn);
    		}
    	}
     
    	System.out.print("You entered: ");
    	System.out.println( mbin.toString() );
     
    	int p = 3;
    	for(int i = 0; i < (mbin.size() - 1); i++)
    	{
    		String s = mbin.elementAt(i);
    		char c = ' ';
    	}
     
    }
    }
    Last edited by yacek; October 21st, 2011 at 11:17 PM.

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Cannot find symbol ERROR

    I can't figure it out, here's a totally simplified example I tried to compile. Same error occurs at the line where I have String s = mbin.elementAt(i);
    See the API for ArrayList.
    ArrayList (Java Platform SE 6)
    There is no method elementAt, or addElement (you are probably looking for get(int) and add(int))

  10. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol ERROR

    Quote Originally Posted by copeg View Post
    See the API for ArrayList.
    ArrayList (Java Platform SE 6)
    There is no method elementAt, or addElement (you are probably looking for get(int) and add(int))
    THANK YOU! I was using vector methods *facepalm*

Similar Threads

  1. error :cannot find symbol
    By iswan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 1st, 2011, 08:26 AM
  2. NEW TO PROGRAMMING, PLEASE HELP (cannot find symbol error)
    By bluescholar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 26th, 2011, 04:48 PM
  3. Cannot find symbol error
    By AnuR in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 23rd, 2011, 02:50 PM
  4. cannot find symbol Error
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2010, 02:36 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM