Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By javan0ob

Thread: need help with program

  1. #1
    javan0ob is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default need help with program

    before you make fun of me, i just started learning java last week.

    so im trying to write a basic program that asks you your height then outputs a line depending on the number you put in. my problem is i dont think im writing the part where it checks if the value you input is greater or lower than a number then outputs a line accordingly. heres my code:

    Java Code:
    import java.util.Scanner;
    public class ex2
    {
    	public static void main (String[] args) 
    	{
    		Scanner key = new Scanner(System.in);
    		
    		double heightIn;
    		final int x = 6;
    		
    		System.out.print("How tall are you? ");
    		heightIn = key.nextInt();
    		
    		double heightOut = heightIn / 12;
    		
    		if(heightOut > x);
    		{
    			System.out.println(+ heightOut + "in, wow you're short");
    			
    				else if(heightOut < x);
    				{
    					System.out.println(+ heightOut + "in, wow you're tall");
    				}
    		}
    		
    	}
    }
    i keep getting an error stating "else without if"
    please help

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

    Default Re: need help with program

    Quote Originally Posted by javan0ob View Post
    before you make fun of me, i just started learning java last week.

    so im trying to write a basic program that asks you your height then outputs a line depending on the number you put in. my problem is i dont think im writing the part where it checks if the value you input is greater or lower than a number then outputs a line accordingly. heres my code:

    Java Code:
    import java.util.Scanner;
    public class ex2
    {
    	public static void main (String[] args) 
    	{
    		Scanner key = new Scanner(System.in);
    		
    		double heightIn;
    		final int x = 6;
    		
    		System.out.print("How tall are you? ");
    		heightIn = key.nextInt();
    		
    		double heightOut = heightIn / 12;
    		
    		if(heightOut > x);
    		{
    			System.out.println(+ heightOut + "in, wow you're short");
    			
    				else if(heightOut < x);
    				{
    					System.out.println(+ heightOut + "in, wow you're tall");
    				}
    		}
    		
    	}
    }
    i keep getting an error stating "else without if"
    please help
    You've got two problems with your if/else code that I can see immediately.

    1) You have a semi-colon at the end of your if boolean condition here:
    Java Code:
    if(heightOut > x);
    This is the same as doing something like this:
    Java Code:
    if(heightOut > x)
    {
       ;
    }
    Where the code that is controlled by the if's boolean condition is an empty line of code. All code below this, including the block of code that you thought would be controlled by the if condition, will be called regardless. So this code will be called no matter if heightOut > x or not:

    Java Code:
    		{
    			System.out.println(+ heightOut + "in, wow you're short");
    			
    				else if(heightOut < x);
    				{
    					System.out.println(+ heightOut + "in, wow you're tall");
    				}
    		}
    Your other problem is that you have an else that doesn't match up with any preceding if, and that's where the compiler is complaining. Check your code and you'll see there's no if block immediately preceding the else, on the same scope level as the else that matches up with it. You need to have something like:

    Java Code:
    if (something) 
    {
        //... some code here
    }
    else 
    {
       // some other code here...
    }
    Even if you didn't have that semicolon on your if line What you've got something like this:

    Java Code:
    if (something) 
    {
        //... some code here
    
        // there's no if here to match the else and on the same level as the else
        else 
        {
           // some other code here...
        }
    }

  3. #3
    javan0ob is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 01:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 07:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Tags for this Thread

Posting Permissions

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