This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if/else selection structure

 
Dave Kairys
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic