DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   I can't display the right output (else if statement) (http://www.daniweb.com/forums/thread89012.html)

iCez Sep 9th, 2007 3:22 am
I can't display the right output (else if statement)
 
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:


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");
                }
        }
}

ede Sep 9th, 2007 4:29 am
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");
}

iCez Sep 9th, 2007 4:38 am
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" =(

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. >.<

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");
  }

peter_budo Sep 9th, 2007 5:44 am
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
    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
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");
                }
        }
}

iCez Sep 9th, 2007 5:57 am
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. ^^


All times are GMT -4. The time now is 8:46 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC