I can't display the right output (else if statement)

Thread Solved
Reply

Join Date: Sep 2007
Posts: 13
Reputation: iCez is an unknown quantity at this point 
Solved Threads: 0
iCez iCez is offline Offline
Newbie Poster

I can't display the right output (else if statement)

 
0
  #1
Sep 9th, 2007
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:


  1. import java.io.*;
  2. public class Test5
  3. {
  4. public static BufferedReader input=new BufferedReader (new InputStreamReader(System.in));
  5. public static void main(String args[])
  6. {
  7. String Department;
  8. int dept=0;
  9. try{
  10. System.out.println("Enter Department:");
  11. Department=input.readLine();
  12. }
  13. catch(IOException ioe) {}
  14.  
  15. if (dept<=9){
  16. System.out.println("You belong to: Mr.Z");
  17. }else if (dept<=7){
  18. System.out.println("You belong to: Mr.Y");
  19. }else if (dept<=3){
  20. System.out.println("You belong to: Mr.X");
  21. }
  22. }
  23. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 9
Reputation: ede is an unknown quantity at this point 
Solved Threads: 2
ede's Avatar
ede ede is offline Offline
Newbie Poster

Re: I can't display the right output (else if statement)

 
0
  #2
Sep 9th, 2007
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");
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 13
Reputation: iCez is an unknown quantity at this point 
Solved Threads: 0
iCez iCez is offline Offline
Newbie Poster

Re: I can't display the right output (else if statement)

 
0
  #3
Sep 9th, 2007
@ede
I tried your suggestion, but the output always display "you belong to Mr.X" =(

  1. import java.io.*;
  2. public class Test5
  3. {
  4. public static BufferedReader input=new BufferedReader (new InputStreamReader(System.in));
  5. public static void main(String args[])
  6. {
  7. String Department;
  8. int dept=0;
  9. try{
  10. System.out.println("Enter Department:");
  11. Department=input.readLine();
  12. }
  13. catch(IOException ioe) {}
  14.  
  15. if (dept>7&&dept<=9){
  16. System.out.println("You belong to: Mr.Z");
  17. }else if (dept>3&&dept<=7){
  18. System.out.println("You belong to: Mr.Y");
  19. }else if (dept<=3){
  20. System.out.println("You belong to: Mr.X");
  21. }
  22. }
  23. }

I also tried, but the error is still the same. >.<

  1. if (dept<=3){
  2. System.out.println("You belong to: Mr.X");
  3. }else if (dept>=4&&dept<=7){
  4. System.out.println("You belong to: Mr.Y");
  5. }else if (dept>=8&&dept<=9){
  6. System.out.println("You belong to: Mr.Z");
  7. }
Last edited by iCez; Sep 9th, 2007 at 4:43 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,114
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 470
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: I can't display the right output (else if statement)

 
2
  #4
Sep 9th, 2007
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
    1. if (dept<=3){
    2. System.out.println("You belong to: Mr.X");
    3. }else if (dept>=4&&dept<=7){
    4. System.out.println("You belong to: Mr.Y");
    5. }else if (dept>=8&&dept<=9){
    6. System.out.println("You belong to: Mr.Z");
    7. }
  • Another option is code bellow which also deal with number out of boundaries 1 - 9
  1. import java.io.*;
  2. public class Test5
  3. {
  4. public static BufferedReader input=new BufferedReader (new InputStreamReader(System.in));
  5. public static void main(String args[])
  6. {
  7. String Department;
  8. int dept=0;
  9. try{
  10. System.out.println("Enter Department:");
  11. Department=input.readLine();
  12. dept = (int) Integer.parseInt(Department);
  13. }
  14. catch(IOException ioe) {}
  15.  
  16. if(dept>7 && dept<10){
  17. System.out.println("You belong to: Mr.Z");
  18. }else if (dept>3 && dept<8){
  19. System.out.println("You belong to: Mr.Y");
  20. }else if (dept>0 && dept<4){
  21. System.out.println("You belong to: Mr.X");
  22. }
  23. else{
  24. System.out.println("Department not recognice");
  25. }
  26. }
  27. }
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 13
Reputation: iCez is an unknown quantity at this point 
Solved Threads: 0
iCez iCez is offline Offline
Newbie Poster

Re: I can't display the right output (else if statement)

 
0
  #5
Sep 9th, 2007
@peter_budo
thx for the help! Actually I didn't forgot the parse thing, I just don't know how it works. >.< We we're not yet taught about that. ~.~

Thx anyway, it was a big help. ^^
Last edited by iCez; Sep 9th, 2007 at 5:58 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC