Results 1 to 3 of 3
Thread: need help with program
- 11-17-2011, 11:33 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
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:
i keep getting an error stating "else without if"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"); } } } }
please help
-
Re: need help with program
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:
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:if(heightOut > x) { ; }
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:{ System.out.println(+ heightOut + "in, wow you're short"); else if(heightOut < x); { System.out.println(+ heightOut + "in, wow you're tall"); } }
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 } else { // some other code here... }
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... } }
- 11-18-2011, 12:10 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks