I create a method for Scanner:
public static char getStatus() {
Scanner kb = new Scanner(System.in);
System.out.print("You are in state student or not(yes for Y/y,no for N/n)");
char state = kb.nextLine().charAt(0);
if (state == 'Y'||state == 'y') {
String instate = "In State";
System.out.println(instate);
int statusfee = 5;
return instate;
} else if (state == 'N'||state == 'n') {
String outstate = "Out-of-State";
System.out.println(outstate);
int statusfee = 5+2;
return outstate;
} else {
String false = "FALSE";
System.out.print(false);
return false;
}
}
I try to print out for my choice. For Example:
System.out.println("Residencey:" + state);
I want when I type "Y or y" then the output is Residencey: In State.
And the Errors are:
error: incompatible types: String cannot be converted to char
I don't know why, does java cannot return String or sentence? I'm a beginner in java, So I cannot use some difficult method.
If your requirement is char status = getStatus();
I think your Class look like:
public class ScannerTest {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
char status = getStatus(kb);
if (status == 'Y') {
// System.out.println("In State");
} else if (status == 'N') {
// System.out.println("Out-of-State");
} else {
// System.out.println("FALSE");
}
System.out.println("Status is " + status);
}
public static char getStatus(Scanner kb) {
System.out
.print("You are in state student or not(yes for Y/y,no for N/n)");
char state = kb.nextLine().charAt(0);
if (state == 'Y' || state == 'y') {
String instate = "In State";
System.out.println(instate);
int statusfee = 5;
return 'Y';
} else if (state == 'N' || state == 'n') {
String outstate = "Out-of-State";
System.out.println(outstate);
int statusfee = 5 + 2;
return 'N';
} else {
String myfalse = "FALSE";
System.out.print(myfalse);
return 'F';
}
}
}
I hope it will help you :)