Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Strange problem with else if statements

  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Strange problem with else if statements

    I'm working on some code for an assignment, just a simple one, and it uses some if statements to print some numbers and stupid words, but I don't think the problem is with the code its self, because just the 'else' in an else if statement is underlined red, as if it were an error, and the program I use wants me to delete it. I am quite confused as to the problem, because I know that there aren't any spelling errors that could cause it, and there aren't any errors anywhere else, although it does move to the next else if statement if I remove the first.
    Java Code:
    import java.io.*;
    public class FizzBizz {
    
    	public static void main(String[] args) {
    		for(int num = 1;num<=100;num++)
    		{
    			if(num%3==0);
    				System.out.println("Fizz");
    			else if(num%5==0) 
    				System.out.println("Buzz");
    			else if(num%15==0)
    				System.out.println("FizzBizz");
    			else
    				System.out.println(num);
    		}
    	}
    }
    thanks for any help in advance
    Last edited by herpeslurpy; 01-27-2013 at 05:48 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Strange problem with else if statements

    Quote Originally Posted by herpeslurpy View Post
    I'm working on some code for an assignment, just a simple one, and it uses some if statements to print some numbers and stupid words, but I don't think the problem is with the code its self,
    Whenever I've thought that I was smarter than the compiler, I'm usually wrong -- and so are you. Believe the compiler, it's usually right, and the problem is in fact with the code itself.

    because just the 'else' in an else if statement is underlined red, as if it were an error,
    No "if's", yes there's an error. You need to show us which line is showing the error by a comment in your code.

    and the program I use wants me to delete it. I am quite confused as to the problem, because I know that there aren't any spelling errors that could cause it, and there aren't any errors anywhere else, although it does move to the next else if statement if I remove the first.
    Look at that line and the line before.

    Java Code:
    import java.io.*;
    public class FizzBizz {
    
    	public static void main(String[] args) {
    		for(int num = 1;num<=100;num++)
    		{
    			if(num%3==0);  // ********** I see the error ******
    				System.out.println("Fizz");
    			else if(num%5==0)
    				System.out.println("Buzz");
    			else if(num%15==0)
    				System.out.println("FizzBizz");
    			else
    				System.out.println(num);
    		}
    	}
    }
    Note the line that I've commented. Should there be a semi-colon there?

    Your code is equivalent to:

    Java Code:
    if(num%3==0)
    {
       ;  
    }
    System.out.println("Fizz");
    else if(num%5==0)  // an else with no preceding if.
    {
      //...
    }
    Solution: get rid of that errant semi-colon on your if line.

    Also *always* enclose *ALL* blocks in curly braces, even and especially if they're one line long.
    Last edited by Fubarable; 01-27-2013 at 05:42 PM.

  3. #3
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Strange problem with else if statements

    Solution: get rid of that errant semi-colon on your if line.

    Also *always* enclose *ALL* blocks in curly braces, even and especially if they're one line long.
    Thanks, I feel pretty dumb for not noticing that before, it works like a charm now.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Strange problem with else if statements

    You're quite welcome!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Strange problem with else if statements

    Quote Originally Posted by herpeslurpy View Post
    Thanks, I feel pretty dumb for not noticing that before, it works like a charm now.
    No it doesn't; if a number can be divided by 15 it can also be divided by 3 ... (check your output)

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Strange problem
    By djokovic in forum AWT / Swing
    Replies: 5
    Last Post: 07-07-2012, 12:20 PM
  2. Very Strange Problem related To GUI
    By ravjot28 in forum AWT / Swing
    Replies: 8
    Last Post: 05-18-2010, 03:06 PM
  3. strange problem
    By dinosoep in forum New To Java
    Replies: 6
    Last Post: 05-01-2010, 10:28 AM
  4. Strange Problem
    By Aseem in forum JDBC
    Replies: 5
    Last Post: 02-12-2010, 07:46 AM
  5. Strange problem
    By Tamir in forum Eclipse
    Replies: 1
    Last Post: 08-18-2008, 09:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •