aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList problems" Watch "ArrayList problems" New topic
Author

ArrayList problems

Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
OK so I'm a Newbie but am rather pleased that I've been able to succesfully code all the tutorials in this, rather excellent, suite of tutorials all the way up to ArrayLists.

The specific ArrayList tutorial is here but I'm already getting errors with this code:-


Errors appear on lines 2, 7, 8, 9 and 10.
Error line 2 = arraylist.ArrayList is already defined in this compilation unit.
Errors line 7-10 = cannot find symbol, symbol: method add(String), location: variable listTest of type ArrayList.

Anyone got any clues as to what is happening?
My thanks
R
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

Roger Wolfenden wrote:Anyone got any clues as to what is happening?

Yup. Duplicate names.

Call your class something different (ArrayListTest?), and all those problems will disappear.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
Well excellent speedy response Winston.

Though if anyone else is following this there was still an error after Winston's useful input and that is in line 3 which should not be declared as public. So the correct code now looks like:-

Thanks again
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Roger Wolfenden wrote:
Though if anyone else is following this there was still an error after Winston's useful input and that is in line 3 which should not be declared as public. So the correct code now looks like:-


A more appropriate fix would have been to rename the .java file to ArrayListTest.java.

Typically the class containing your main() will be public. Top-level public classes have to be in a source file that matches the class name.
Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
Well you've got me confused now and have to admit that even after accepting Winston's suggestion, changing the class name to ArrayListTest (thanks Jeff) and completing the tutorial I got a compile error - Error: Could not find or load main class arraylist.ArrayList.

As soon as I saw that I knew it was something to do with the first few lines of code so I'm not quite sure what to do now.

My completed tutorial code looks like this now.


I'm using NetBeans and my ArrayList / Source Packages / arraylist/ file is called ArrayList.java should this be ArrayListTest.java?

Confused, I was really pleased with my progress on this suite of tutorials as I'd had all sorts of similar issues with non working tutorials elsewhere and I don't really understand the relationship between package names, class names and the final src .java files.

It's probably a cinch for you experts but a maze for me.
Your help would be appreciated.
Thanks for reading.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Roger Wolfenden wrote:- Error: Could not find or load main class arraylist.ArrayList.
...
I'm using NetBeans and my ArrayList / Source Packages / arraylist/ file is called ArrayList.java should this be ArrayListTest.java?


Yes.

Source file should be ArrayListTest.java.

Class name declaration inside that file should be public class ArrayListTest.

In your "Run Configuration" setup (or whatever NB calls that particular option), you may need to explicitly tell it that the class you're running is called ArrayListTest. It seems that it still thinks you're trying to run the ArrayList class. I don't use NB, so I don't know where that particular setting is.

Many people (including me) recommend that beginners don't use and IDE like NB or Eclipse, and instead just use a programmer's editor (I think SlickEdit, UltraEdit, and TexPad+ or something like that) and use the command line for compiling or running.
Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
Well thanks Jeff, appreciate that and I'm off to experiment.

On the use of NetBeans, funnily enough I'm two timing the tutorials that I'm doing Tutorials Here as I've also spent the last few days reading a book, "Head First Java" which recommends the text editor approach as you do yourself which is good as it forces you to understand all the detailed issues like closing curly braces etc. etc. but hasn't adequately covered off all the gramatical stuff about class names, packages etc. etc.

Just to put it in prespective, last year I decided to formally study JavaScript and ended up with a (UK) qualification in JavaScript so all the standard Java stuff like - if (){ //Do something} else {//Do something else} and lots and lots of etceteras to this example seems like a doddle to me. What is not a doddle though is all the naming conventions of classes, packages and methods.

My Head First Java book is approx 700 pages long and the first 100 pages or so deal with all the programming grammar conventions and is a complete nightmare to understand whereas the last 600 pages that deal with all the regular operator stuff look quite straightforward.

Thanks again Jeff, appreciated.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome.

And don't feel bad or get discouraged about the naming and classpath stuff. The rules are actually fairly simple and straightforward, but they can be hard to explain clearly, and a little tricky to grasp until that "A-ha!" moment.
 
wood burning stoves
 
subject: ArrayList problems