Results 1 to 7 of 7
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default 'else' without 'if' error message

    Can someone please tell me why I am getting an 'else' without 'if' error message when I try to compile the following code:

    Java Code:
    public class Example3 {
    
        public static void main(String [] args){
    
    		int nums[] = new int[50];
    
    		for(int i = 0; i < 50; i++){
    
    			if(i % 2 == 0){
    				nums[i] = 1;
    
    				else{
    					nums[i] = 0;
    
    					System.out.println(nums[i]);
    				}
    			}
    		}
        }
    }
    I can't figure out what I am doing wrong. It states that the problem is at the else line

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jim01 View Post
    Can someone please tell me why I am getting an 'else' without 'if' error message when I try to compile the following code:

    Java Code:
    public class Example3 {
    
        public static void main(String [] args){
    
    		int nums[] = new int[50];
    
    		for(int i = 0; i < 50; i++){
    
    			if(i % 2 == 0){
    				nums[i] = 1;
    
    				else{
    					nums[i] = 0;
    
    					System.out.println(nums[i]);
    				}
    			}
    		}
        }
    }
    I can't figure out what I am doing wrong. It states that the problem is at the else line
    Count your curly brackets and b.t.w. fix your indendation because as it is now it stinks.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Count your curly brackets and b.t.w. fix your indendation because as it is now it stinks.

    kind regards,

    Jos
    I have an open and closed curly brace for my class, an open and closed curly brace for my method, an open and closed curly brace for my for loop, and one each for my if statement and my else statement. Going back over my notes, each one of these should have open and closed curly braces. Am I mistaken on that? The book only shows three open and closed curly braces for most of its examples, one for the class, one for the method and one for the for loop. It leaves off the curly braces for the if and else statements, but I though they done that because you are not required top have them if you only have one. However, a couple of people on the forum in the past have told me that you should always use curly braces even if you are only going to use an if/even statement once or else you will likely forget to put one in when you need it.



    Java Code:
    public class Example3 {
    
        public static void main(String [] args){
    
    	int nums[] = new int[50];
    
    	for(int i = 0; i < 50; i++){
    
    	    if(i % 2 == 0){
    	       nums[i] = 1;
    
    		else{
    		   nums[i] = 0;
    
    		     System.out.println(nums[i]);
    	       }
    
    	   }
    	}
        }
    }
    Sorry, it moved everything when I cut and pasted it. This is how it should have appeared. Is that better?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jim01 View Post
    Java Code:
    public class Example3 {
    
        public static void main(String [] args){
    
    	int nums[] = new int[50];
    
    	for(int i = 0; i < 50; i++){
    
    	    if(i % 2 == 0){
    	       nums[i] = 1;
    
    		else{
    		   nums[i] = 0;
    
    		     System.out.println(nums[i]);
    	       }
    
    	   }
    	}
        }
    }
    Sorry, it moved everything when I cut and pasted it. This is how it should have appeared. Is that better?
    You forgot the second part of my remark: you indentation stinks; an if-else statement is supposed to look like this:

    Java Code:
    if (<condition>) {
       <statement>
    }
    else {
       <statement>
    }
    Fix your indentation and most likely you'll see your mistake. b.t.w. my parts between < and > are just paceholders.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You forgot the second part of my remark: you indentation stinks; an if-else statement is supposed to look like this:

    Java Code:
    if (<condition>) {
       <statement>
    }
    else {
       <statement>
    }
    Fix your indentation and most likely you'll see your mistake. b.t.w. my parts between < and > are just paceholders.

    kind regards,

    Jos
    I see where I was making my mistake. I was using the curly brackets for the if/else statements as you would a loop statement.

    Thank you for the help and correcting my on my indentation.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jim01 View Post
    I see where I was making my mistake. I was using the curly brackets for the if/else statements as you would a loop statement.

    Thank you for the help and correcting my on my indentation.
    Your welcome of course but hold on for a second: a for-loop doesn't have different curly brackets from, say, an if-statement; have a look:

    Java Code:
    for (<for-loop-header>) {
       <statement>
    }
    
    if (<condition>) {
       <statement>
    }
    They're all the same, see?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Your welcome of course but hold on for a second: a for-loop doesn't have different curly brackets from, say, an if-statement; have a look:

    Java Code:
    for (<for-loop-header>) {
       <statement>
    }
    
    if (<condition>) {
       <statement>
    }
    They're all the same, see?

    kind regards,

    Jos
    Yes, but from my understanding you incorporate the if statements into the loop by adding the if statement before the loops closing curly brace. For example:

    Java Code:
    public class PracticeExample2A {
    
        public static void main(String[] args){
    
        	//declare variable sum
        	int evenSum = 0;
        	int oddSum = 0;
    
        	//perform for loop
        	for(int i = 1; i <= 1000; i++){
    
        	     if(i % 2 == 0){
        		evenSum += i;
        	     }
        	     else{
        		oddSum += i;
        	     }
    
            }
    		//output
        	System.out.println("The sum of all even numbers is " + evenSum);
        	System.out.println("The sum of all odd numbers is " + oddSum);
        }
    }
    I was trying to do the same thing with the if and else statements, which of course was wrong.
    Last edited by jim01; 04-22-2011 at 09:52 PM.

Similar Threads

  1. TicTacToe Error Message
    By Ryan10 in forum New To Java
    Replies: 12
    Last Post: 03-30-2011, 05:51 PM
  2. error message
    By tri.yudhanto in forum JDBC
    Replies: 0
    Last Post: 01-13-2011, 01:20 PM
  3. Error Message ..
    By Hamodi18 in forum New To Java
    Replies: 15
    Last Post: 07-11-2010, 03:31 AM
  4. Error Message????
    By Cubba27 in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:46 PM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 AM

Posting Permissions

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