• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with 'else without if' error  RSS feed

 
Nafisa Faruque
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am absolutely new to programming languages and Java. So I have an assignment in which I need help. It shows an 'else without if' error on the 'else' line:


 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Your else must be exactly one statement after the if. It is one statement and a { after the if.
you should also indent your code so the else is exactly the same distance from the left as the if. You can do that by writing backwards, as I showed here. Always put { and } after if, else, etc. That groups all your statements into a block which the compiler sees as equivalent to a single statement.

I added code tags to your post: always use them. Doesn't it look better Remember you should indent your }s as well as the {s.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When you post code, please UseCodeTags (← Cclick). I've added them for you this time.

When you have a lot of if/then/elses as you have, it's really important to indent correctly. You should pick a style -- it looks like you should use this one:



If you do this to your code, you'll see the problem.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I first tried the code tags it appeared that the }s were not indented. Now they do appear indented. You can now see, just by looking at the code, however, that there are more }s than there are {s. That will produce an interesting compiler error
 
Nafisa Faruque
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Your else must be exactly one statement after the if. It is one statement and a { after the if.
You should also indent your code so the else is exactly the same distance from the left as the if. You can do that by writing backwards, as I showed here. Always put { and } after if, else, etc. That groups all your statements into a block which the compiler sees as equivalent to a single statement.

I added code tags to your post: always use them. Doesn't it look better Remember you should indent your }s as well as the {s.


Hi. Thanks so much for your prompt reply! I didn't fully understand what you meant. I know I sound a little silly but I actually learnt how to code some simple tasks in Java only today. Could you kindly explain how I can fix the error once again? I'm posting my assignment question for easier reference:
Task 7:
Write a java program that reads an integer, and if the number is even and greater than 10, prints “An even number greater than 10”. If the number is even but lesser than 10, print “An even number not greater than 10”. If the number is greater than 10 but odd, print “An odd number greater than 10”. If the number is odd and also less than 10, print “An odd number less than 10”.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write if statements, it's a good practice to start with just the first one like this:



Then add your next level...



So you would start:



then



Keep on going like that and your curly braces will be lined up (matched).
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nafisa Faruque wrote:
Task 7:
Write a java program that reads an integer, and if the number is even and greater than 10, prints “An even number greater than 10”. If the number is even but lesser than 10, print “An even number not greater than 10”. If the number is greater than 10 but odd, print “An odd number greater than 10”. If the number is odd and also less than 10, print “An odd number less than 10”.


Looking at the task, you need to check 2 things: 1) is number even or odd, 2) is number < or > than 10. Now I wonder what will happen if you input "10". Hmm

Have you learn about methods? Using methods tend to reduce the nesting if statements. You can check 2 things with one if statement using logical AND operator &&

 
Nafisa Faruque
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Knute Snortum
Thanks a bunch!!
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nafisa Faruque wrote:Hi I am absolutely new to programming languages and Java. So I have an assignment in which I need help. It shows an 'else without if' error on the 'else' line:


At first glance if you face the brace on line 25 the other way it will fix it. Several good explanations here about braces. If you use an IDE like Eclipse you can click on one brace and it will highlight its mate.

Also, it's easier to take in if you only indent a maximum of 4 spaces. Also also it's good to not have conditions nested so deep (because it's harder to understand). You can have condtions in sequence and return if the condition isn't met, instead of nesting the conditions. Or just change the logic.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One good principle to remember is Don't Repeat Yourself (DRY) - lines 12 and 22 are the same. Lines 10 and 20 are practically the same too since they're on opposite sides of an if-else statement. Those lines are duplicates and redundant.

You really only have two conditions you want to check: Whether or not the number is even and whether or not the number is greater than 10. Thus, you should have only two if-else statements, one for each of those conditions. You can control your output using System.out.print and System.out.println at the right time.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might do well to start by printing whether the number is greater than 10 or not. Forget about odd and even for the time being; you can do that later.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!