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

<identifier> expected

 
Eric Rickshaw
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys,

I apprciate everybody's help. Thanks for making me feel welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic