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 if/else selection structure 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 "if/else selection structure" Watch "if/else selection structure" New topic
Author

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{
daysLessThan31.add(myMonth[x].Name);System.out.println((String)daysLessThan31.get(x));
}
x++;
}
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?
Thanks!
Sunil Kumar Gupta
Ranch Hand

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

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


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
Bartender

Joined: Oct 02, 2003
Posts: 11166
    
  16

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: if/else selection structure
 
Similar Threads
how to test the setter methods
Out of bounds exception in code
incrementing arrays
Strange behavior of the Calendar get() method
A difficult OOD question...