This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes <identifier> expected 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 "<identifier> expected" Watch "<identifier> expected" New topic
Author

<identifier> expected

Eric Rickshaw
Greenhorn

Joined: Jan 17, 2009
Posts: 3
Hey guys.

I'm new to the saloon. My name is Eric. I was wondering if you guys could help me with a program I've been working on. I had my program working with various strings, but I decided to rework it with array lists. The problem is the 17 or so instances of

matrix.add(poem#);
poem#.add(Author);
poem#.add(Title);

I have gives me an identifier expected error. I tried using the just section of the array lists exactly as it appears below in its own class and it worked so Im guessing where the arraylists are placed below they may be in the wrong section of the code. If someone could give me an idea as where to put my arraylists in the context of my program (that Ive included below) I would greatly appreciate it. Thank you.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Eric Rickshaw wrote:so Im guessing where the arraylists are placed below they may be in the wrong section of the code.

It appears to me to be a simple case of trying to call methods outside of a method, constructor, static or initializer block. You can't call code like this:

in the class outside of a method, constructor, etc. This is just basic Java, is all.

Edit: there's more involved here. I agree with your use of ArrayList here but don't agree with how you are attempting to implement it as you appear to be trying to create a separate ArrayList for each poem. If it were my app, I'd create a class to hold Poem information, say called "Poem", that would hold a String for the author, a String for the title, and a String for the text of the poem. It would have the typical getter methods. I'd then create one and only one arraylist of Poem that would hold all of the poems: ArrayList<Poem>. YMMV.
Eric Rickshaw
Greenhorn

Joined: Jan 17, 2009
Posts: 3
Hey pete,

Thanks for your help. Yeah I'm wondering how to position the arraylists. I can see why you would think it might be useful to use strings but I will be adding to each arraylist the lines of each poem. Each line will be in a seperate parrt of the array list. I will generate two random numbers. One will say which of the poems in the big array list is to be selected and then another to generate a random line based on the length of the poem. The line of poetry is displayed in a GUI and the expected values for that poem (title and author) will be set to two variables based on the first and second component of the arraylist for each poem. My intention is two remove a line of poetry from the arraaylist if it is guessed correctly. The code that worked was as followed. Thank you.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I still strongly suggest that you create a Poem class and use only one ArrayList<Poem>. A big confusion that new programmers make is to confuse data with code. Your data is the poem information, currently showing title and author. You want the data to be changeable, flexible, alterable, and you want the code to be able to manipulate the data as needed. Right now you are hard-coding your data each into its own tiny arraylist, and when you do this, your losing information and painting yourself into a corner.

If instead you create a Poem class, each bit of a poem's information goes into its proper variable: the title goes into the title field, the poem's name into the name field, and it's all easily and logically extractable. If you want to add 15 or 100 poem's to the ArrayList, it is easy to do, and the same for deleting them.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

pete stein wrote:I still strongly suggest that you create a Poem class and use only one ArrayList<Poem>. A big confusion that new programmers make is to confuse data with code. Your data is the poem information, currently showing title and author. You want the data to be changeable, flexible, alterable, and you want the code to be able to manipulate the data as needed. Right now you are hard-coding your data each into its own tiny arraylist, and when you do this, your losing information and painting yourself into a corner.

If instead you create a Poem class, each bit of a poem's information goes into its proper variable: the title goes into the title field, the poem's name into the name field, and it's all easily and logically extractable. If you want to add 15 or 100 poem's to the ArrayList, it is easy to do, and the same for deleting them.


Not to mention the ability to organize the data in a meaningful way - ie sort by author, or poem name, etc...

A simple Poem class might look something like this:


This would help organize your data, sort, get a particular line you want from the poem, etc...


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Steve Luke has given some useful advice; I would simply add a warning against writing == true or == false. You can get nasty errors if you write = instead of == by mistake.
Don't write if (something() == false) . . .
Write if (!something()) . . .

Similarly don't write if (myBoolean == false) . . .
Write if (!myBoolean) . . .
Eric Rickshaw
Greenhorn

Joined: Jan 17, 2009
Posts: 3
Thank you guys,

I apprciate everybody's help. Thanks for making me feel welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <identifier> expected
 
Similar Threads
I Seriously Need Helps.. Ugently
Dice Game help and Arrays
Multidimensional ArrayList
Error in NapKin Look and Feel
JDialog Opens Very Tiny