wood burning stoves*
The moose likes Beginning Java and the fly likes Vectors and Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vectors and Loops" Watch "Vectors and Loops" New topic
Author

Vectors and Loops

Danny Morgan
Greenhorn

Joined: Jul 05, 2008
Posts: 8
Hello all!

I've been trying to compile the code below but I keep getting the same error message and I don't know how to correct it can some one help me!



public StringLister(String[] moreNames) {// Does this mean create a method called StringLister with the String array moreNames as its argument?





I get the error the following message when I try to compile the program.

'(' or '[' expected.
[ July 09, 2008: Message edited by: Campbell Ritchie ]

Regards<br /> <br />Danny
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

your code compiled and ran just fine for me. are you SURE this is what you're compiling when you get the error?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Code is working fine for me.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Sorry for the delay; my wireless connection went down halfway through my post.
Originally posted by Danny Morgan:
public StringLister(String[] moreNames) {// Does this mean create a method called StringLister with the String array moreNames as its argument?
No, that is the constructor; you are correct about a String array which you are calling moreNames. That array is the arguments array from the main method with a different name; you are passing it from main to the constructor.

I have edited your post and put the code tags where I thought they ought to be. In fact what is now between the two code tags compiles and runs at the first attempt; you must have corrected the error when you copied the code.

To use the command line arguments you can run it like this

prompt>> java StringLister Danny Campbell

and it will print Campbell and Danny in the appropriate locations.
Why are you doing so much work in the constructor? You would do better to have the list as a field, then call a sort() method then a print() method. You would end up with this:Apart from declaring your list to be of the type List, I haven't changed anything, only moved it into different locations.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Which version of Java are you using? Generics (the < and > angle brackets stuff) was added in Java 5. If you are using Java 1.4 or older, you'll get errors.

Note: Vector is a really old class (from Java 1.1), which has been replaced by ArrayList. Use ArrayList instead of Vector.

Are you working from a really old book or tutorial? A good online set of tutorials can be found here: Sun Java Tutorials.
[ July 10, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Danny Morgan
Greenhorn

Joined: Jul 05, 2008
Posts: 8
Thanks to all who replied to my post.
I'm working through the Sams Teach Yourself Programming In 24 Hours Fourth Edition, with Java 2 Version 5. Using Blue J 1.3.5
Is the problem with the version of Blue J that I'm using?

Thanks Jesper Young I've downloaded the Sun Java Tutorials. What do you think of the book that I'm using?
Danny Morgan
Greenhorn

Joined: Jul 05, 2008
Posts: 8
Just downloaded the latest version of Java and Blue J and it compiled ok. Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Not sure, but the most recent version of BlueJ seems to be 2.2.1, so I would suggest you update. Difficult to be sure with BlueJ, but I have had problems with it failing to recognise newer versions of Java, eg not supporting static import when I was using Java5, so it might be the old version of BlueJ causing the problem.

Although (as Jesper has told you) the Java Tutorial is probably the most comprehensive Java guide anywhere, both it and Cadenhead (Java in 24 hours) tend to put lots of code in the main method, so you never get used to writing separate methods. Go and search through JavaRanch until you find "Bunkhouse Books" and look at the reviews there.

I have downloaded the latest edition of BlueJ and your original class compiles nicely; I see you managed that too while I was writing.
Danny Morgan
Greenhorn

Joined: Jul 05, 2008
Posts: 8
Ok Campbell Ritchie! Thanks for the advice!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Originally posted by Danny Morgan:
Ok Campbell Ritchie! Thanks for the advice!
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Vectors and Loops
 
Similar Threads
ArrayList with Iterator
sublist of arraylist???
custom taglib problem
compiler error-incompatible types
This code is right or wrong ?