| | |
I can't display the right output (else if statement)
Thread Solved
![]() |
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Solved Threads: 0
Hi everyone, I hope you guys can help me.
Output
Enter your department number:
You belong to:
1-3 = Mr.X
4-7 = Mr.Y
8-9 = Mr.Z
------
Error:
When I input numbers from 1-9, the output that is displayed is always "You belong to: Mr. Z"
Here's my code:
Output
Enter your department number:
You belong to:
1-3 = Mr.X
4-7 = Mr.Y
8-9 = Mr.Z
------
Error:
When I input numbers from 1-9, the output that is displayed is always "You belong to: Mr. Z"
Here's my code:
Java Syntax (Toggle Plain Text)
import java.io.*; public class Test5 { public static BufferedReader input=new BufferedReader (new InputStreamReader(System.in)); public static void main(String args[]) { String Department; int dept=0; try{ System.out.println("Enter Department:"); Department=input.readLine(); } catch(IOException ioe) {} if (dept<=9){ System.out.println("You belong to: Mr.Z"); }else if (dept<=7){ System.out.println("You belong to: Mr.Y"); }else if (dept<=3){ System.out.println("You belong to: Mr.X"); } } }
numbers between 1-9 always are <=9. so you must specify an interval inside if-if else clause
like :
if(dept>7 && dept<=9)
System.out.println("You belong to: Mr.Z");
}else if (dept>3 && dept<=7){
System.out.println("You belong to: Mr.Y");
}else if (dept<=3){
System.out.println("You belong to: Mr.X");
}
like :
if(dept>7 && dept<=9)
System.out.println("You belong to: Mr.Z");
}else if (dept>3 && dept<=7){
System.out.println("You belong to: Mr.Y");
}else if (dept<=3){
System.out.println("You belong to: Mr.X");
}
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Solved Threads: 0
@ede
I tried your suggestion, but the output always display "you belong to Mr.X" =(
I also tried, but the error is still the same. >.<
I tried your suggestion, but the output always display "you belong to Mr.X" =(
Java Syntax (Toggle Plain Text)
import java.io.*; public class Test5 { public static BufferedReader input=new BufferedReader (new InputStreamReader(System.in)); public static void main(String args[]) { String Department; int dept=0; try{ System.out.println("Enter Department:"); Department=input.readLine(); } catch(IOException ioe) {} if (dept>7&&dept<=9){ System.out.println("You belong to: Mr.Z"); }else if (dept>3&&dept<=7){ System.out.println("You belong to: Mr.Y"); }else if (dept<=3){ System.out.println("You belong to: Mr.X"); } } }
I also tried, but the error is still the same. >.<
Java Syntax (Toggle Plain Text)
if (dept<=3){ System.out.println("You belong to: Mr.X"); }else if (dept>=4&&dept<=7){ System.out.println("You belong to: Mr.Y"); }else if (dept>=8&&dept<=9){ System.out.println("You belong to: Mr.Z"); }
Last edited by iCez; Sep 9th, 2007 at 4:43 am.
Same mistake in both cases
- you store user input into Department but compared variable is dept, I think you forgot to parse String Department to integer dept so whole time you run check on null value of dept
int dept=0; - your last attempt iCez actually work once parse implemented Java Syntax (Toggle Plain Text)
- if (dept<=3){
- System.out.println("You belong to: Mr.X");
- }else if (dept>=4&&dept<=7){
- System.out.println("You belong to: Mr.Y");
- }else if (dept>=8&&dept<=9){
- System.out.println("You belong to: Mr.Z");
- }
- Another option is code bellow which also deal with number out of boundaries 1 - 9
Java Syntax (Toggle Plain Text)
import java.io.*; public class Test5 { public static BufferedReader input=new BufferedReader (new InputStreamReader(System.in)); public static void main(String args[]) { String Department; int dept=0; try{ System.out.println("Enter Department:"); Department=input.readLine(); dept = (int) Integer.parseInt(Department); } catch(IOException ioe) {} if(dept>7 && dept<10){ System.out.println("You belong to: Mr.Z"); }else if (dept>3 && dept<8){ System.out.println("You belong to: Mr.Y"); }else if (dept>0 && dept<4){ System.out.println("You belong to: Mr.X"); } else{ System.out.println("Department not recognice"); } } }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Display output on webpage (PHP)
- can't display the output! can somebody help me?!! PLS.. (C)
- Program Output Display Tweak Help (Java)
- Am not able to get a radio button selection to display in output (PHP)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- help clear mess in single-scripted array (C++)
- Time display program (C++)
Other Threads in the Java Forum
- Previous Thread: Reading from a file
- Next Thread: Java - to Post on web
| Thread Tools | Search this Thread |
account android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer homework html ide image inheritance integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux loop method midlethttpconnection migrate mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program property ria scanner server set sharepoint smart sms smsspam sourcelabs splash sql sqlite subclass support swing testautomation textfield threads tree trolltech unlimited utility windows






