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

Thread: The dreaded "Error: cannot find symbol" error...

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The dreaded "Error: cannot find symbol" error...

    I'm picking up Java for a class, and having prior C++ knowledge this stuff has been fairly straightforward with the exception of trying to learn the whole IO system in this language. But there seems to one error I just cannot figure out what it's trying to tell me,

    className.java:line_number: error: cannot find symbol
                                            myList.add(input);
                                                       ^

    I have dealing with this error left and right after picking up this language, and I can never seem to figure out what it's trying to tell me. I've heard people say it's referring misspelled objects or methods, but I've double and triple checked my code and that never seems to be true for me.

    I'm trying to make a small program which will allow me to play with the LinkedList object so I can gain some familiarity with how they work in Java, and I have this code so far:

    import java.io.*;
    import java.util.*;
     
    public class playWithLists {
    	public static void main(String[] args) throws Exception {
    		//List<String> myList = new LinkedList<String>();
    		LinkedList myList = new LinkedList();
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		String input = "";
     
    		for (int i = 0; i < 5; i++) {
    			try {
    				input = br.readLine();
    			} catch (Exception e) {
    				System.err.println("Uh oh!");
    			}
    			myList.add(input);
    		}
     
    		for (int i = 0; i < 5; i++) {
    			System.out.println(myList.removeFirst());
    		}
    	}
    }

    The compiler is giving me an error at line 16 (specifically: myList.add(input); )

    Can anyone give me some advice on what I'm missing here? This error is driving me nuts.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: The dreaded "Error: cannot find symbol" error...

    This code compiles fine for me.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: The dreaded "Error: cannot find symbol" error...

    Yeah, I don't see anything wrong with this. A word of caution though, I would put your myList.add() method in the try statement. If the read throws an error, you will be adding the previous value to the LinkedList a second time.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The dreaded "Error: cannot find symbol" error...

    I guess my compiler was being screwy or something then... Anyhow, I think I've made a little headway myself on the error in general, mostly in that the compiler wasn't telling me that the method I was attempting to use didn't exist in that particular object's class.

    Also, with the code I originally posted I'm getting warnings saying that I'm using unsafe or unchecked operations. But I guess I've gotten it figured out for now.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: The dreaded "Error: cannot find symbol" error...

    The warnings are probably because you didn't specify the type of LinkedList:
    LinkedList<String> myList = new LinkedList<>();
    Not a huge deal if you know what your doing. I'm not sure how much the compiler likes that when it comes to optimization though.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: The dreaded "Error: cannot find symbol" error...

    Note that "the compiler being screwy" was probably in fact you not compiling the correct file, or forgetting to save the file after you made a change. It doesn't really matter now that you got it fixed, but automatically blaming the compiler isn't a great habit to get into.

    PS- Sweet name.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. "Error cannot find symbol" "throws BadLocationException"
    By coltson in forum AWT / Java Swing
    Replies: 1
    Last Post: June 30th, 2013, 10:33 PM
  2. "cannot find symbol" error when trying to use the getInt() method.
    By simpson_121919 in forum Collections and Generics
    Replies: 6
    Last Post: February 21st, 2013, 12:48 PM
  3. Replies: 3
    Last Post: January 22nd, 2013, 07:14 AM
  4. Please! Help me to this error "ERROR CANNOT FIND SYMBOL"
    By mharck in forum Object Oriented Programming
    Replies: 8
    Last Post: July 3rd, 2012, 09:20 AM
  5. "Cannot find symbol" compilation error
    By collegejavastudent in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 12th, 2011, 05:07 PM