File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes if/else selection structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if/else selection structure" Watch "if/else selection structure" New topic

if/else selection structure

Dave Kairys
Ranch Hand

Joined: Sep 09, 2005
Posts: 33
Hi. I have the following code:
(more above here)
while (x < myMonth.length){
if(myMonth[x].Days == 31) days31.add(myMonth[x].Name);{System.out.println((String)days31.get(x));
} else{
Upon compiling, it gives the following error message:
'else' without 'if'.
I have scoured my textbook for any answers and all I see is that I have followed the syntax the same way. Any suggestions?
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
It should be......

if(myMonth[x].Days == 31){
}else {
daysLessThan31.add(myMonth[x].Name); System.out.println((String)

see it works

Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

your problem is the semi-colon on this line:

if(myMonth[x].Days == 31) days31.add(myMonth[x].Name);

what you have here is an empty block for your 'if' condition. then you have the block:

{ System.out.println((String)days31.get(x));}

then you have an else with no if...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: if/else selection structure
It's not a secret anymore!