944,015 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1013
  • Java RSS
Sep 9th, 2007
0

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

Expand Post »
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:


Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iCez is offline Offline
13 posts
since Sep 2007
Sep 9th, 2007
0

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

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");
}
ede
Reputation Points: 10
Solved Threads: 2
Newbie Poster
ede is offline Offline
9 posts
since Aug 2006
Sep 9th, 2007
0

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

@ede
I tried your suggestion, but the output always display "you belong to Mr.X" =(

Java Syntax (Toggle Plain Text)
  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. >.<

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iCez is offline Offline
13 posts
since Sep 2007
Sep 9th, 2007
2

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

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)
    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
Java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Sep 9th, 2007
0

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

@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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iCez is offline Offline
13 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Reading from a file
Next Thread in Java Forum Timeline: Java - to Post on web





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC