• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't find symbol variable get - PLEASE HELP!!!

 
Carmel Leaning
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this not work? I am having problems with the buypack method.



Thank you!!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Sorry it has taken so long to reply.

If "get" means a method, it should have round brackets (). It should probably have an argument or arguments too.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you might do well to lose one ( before packets.
 
Carmel Leaning
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks for replying so quickly,

I did that now it is saying cannot find symbol - method get()

Is there another way of writing if a flavour that is entered as a string isn't equal to the flavour declared in the packets then it's null... display error message??

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first: get the method working at all, then refine it later.

What's packets? Is it an array? Has the Packet class got a get() method? What parameters does that get() method require?
Or is there some sort of Map which requires the String called flavour be passed to its get() method?
 
Carmel Leaning
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the packet is in an arraylist, which links up to another class called packofcrisps.

I have done a get method somewhere else in the coding to get the user to enter a flavour and then the machine will see how many of that flavour there are and that works.

Just this one that is being apain, I took that line of coding out and everything else in that method works fine.

Driving me nuts I tell you

Any ideas?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList? Now I know where I am. The get method of the List interface takes an int parameter, so you would have to try packets.get(index).something().
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And were you taught to use a for loop with an index for iterating an ArrayList? When I was at your stage I was taught to use an Iterator. You can see examples here.

Actually, close examination of that last link suggests you are in fact correct to use a straight for-loop for ArrayList.
[ November 07, 2008: Message edited by: Campbell Ritchie ]
 
Carmel Leaning
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did that ((packets.get(index)).getFlavour().equals(flavour))

What to know is how I say not equals, that says if the packet flavour is equals to the flavour entered then display message. how do I say if the packet flavour is NOT equals to the flavour entered?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the bit about if (!...equals(null)) will do what you want.
 
Carmel Leaning
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for trying, it still won't do what I want it to do.

Yeah we have just started to learn about Iterators but I am not good at them yet, I think I am going to have to learn it as it is as I can't seem to find a solution and it needs to be handed on Monday,

Bugger..

Thanks anywho
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it won't work; the loop is up the creek.

You need the RandomAccess interface to show as evidence that you are right to use an index rather than an Iterator.

I am busy now, so I shan't be here for an hour or two. During which time you curse the computer as much as you like, have a cup of tea, get a pencil and paper and write down in words of one syllable what you want to do in that method. Then refine each part in terms the computer can understand. Remember a computer program takes what you tell it literally. That's called pseudo-code.

Is there any risk of null values in your ArrayList? If there is, when you call get() and put a . after it, you will get an Exception.
And don't call equals(null). "null" is a reference and there is only one "null" on your computer, so you can say "== null" or "!= null."
 
Carmel Leaning
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha ha I probably will end up shouting at it!

Changed it



Now it is saying it can't have an else with an if.. ca't win !! Ha ha!
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne has told you how to get out of the if-else problem.

But you really need to work out what that method is supposed to do and how to do it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume by the lack of replies that you have now got it working perfectly?

. . .
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it gives you a compiler error, it will tell you the line where things appear to be wrong. This information is EXTREMELY useful in determining the problem. In the future, please post the entire text of the error message.

If you look at your 'if' statement closely, you have this:

That semicolon on that line closes out the 'if' with an empty block. So when you hit that last 'else', indeed there is no matching 'if'.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by myself:
I presume by the lack of replies that you have now got it working perfectly?

. . .


Did you get it working?
 
Carmel Leaning
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, yes I did fred rosenberger was right I did but a ';' where there shouldn't be one and took that away.... and it works

Thanks for all your help guys!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done getting it to work
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic