Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vectors and Loops

 
Danny Morgan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code compiled and ran just fine for me. are you SURE this is what you're compiling when you get the error?
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code is working fine for me.

 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Danny Morgan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just downloaded the latest version of Java and Blue J and it compiled ok. Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Campbell Ritchie! Thanks for the advice!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Danny Morgan:
Ok Campbell Ritchie! Thanks for the advice!
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic