Hi everyone, I hope you guys can help me.

Output

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 void main(String args[])
{
String Department;
int dept=0;
try{
System.out.println("Enter Department:");
}
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");
}
}
}``````

## All 4 Replies

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
I tried your suggestion, but the output always display "you belong to Mr.X" =(

``````import java.io.*;
public class Test5
{
public static void main(String args[])
{
String Department;
int dept=0;
try{
System.out.println("Enter Department:");
}
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");
}``````

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 void main(String args[])
{
String Department;
int dept=0;
try{
System.out.println("Enter Department:");
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");
}
}
}``````

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.