aspose file tools*
The moose likes Beginning Java and the fly likes Can't add to ArrayList within switch statement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't add to ArrayList within switch statement?

Ray Michaels
Greenhorn

Joined: Apr 09, 2011
Posts: 2
I'm having trouble inserting objects to an ArrayList within a switch statement. Before entering the switch, the add function works fine. Here's the code:



Can anyone advise me as to why the add command within the switch statement isn't working and what to do to make it work?
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi,

Try removing the () from the case number like this case 1: not case (1):


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60074
    
  65

It might be helpful if you explained what "isn't working" means.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ray Michaels
Greenhorn

Joined: Apr 09, 2011
Posts: 2
Nothing gets added to the ArrayList... the size doesn't increase after the add statement within the switch and the values added don't show when I print out the contents of the ArrayList. Don't know how to explain it any other way... Removing parenthesis from around the case values did not fix it .

Edit: I've tried removing the switch and using an if instead, but it does not make a difference.
Edit 2: I figured out the problem was that I was instantiating the ArrayList after everytime I tried to add an appointment within the switch.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't add to ArrayList within switch statement?
 
Similar Threads
Help with printing appointments
getting java.lang.NoSuchMethodError: main error! thanks =)
Making Decisions Lottery application
Noob java assigment
problems with math methods