Reputation: 223
I am writing a little text adventure but I am encountering an error at the beginning. I want to ask the gender of the player, and everything works well, but the message if a wrong gender is written is not getting displayed.
What am I doing wrong?
if (scan.next().equalsIgnoreCase("Frau")) {
System.out.println("Oh eine Dame des" + sClanname + " Clans");
sGender = scan.next();
} else if (scan.next().equalsIgnoreCase("Mann")) {
System.out.println("Oh ein Herr des" + sClanname + " Clans");
sGender = scan.next();
} else {
System.out.println("Entschuldige, wir sind hier sehr konservativ...Es gibt nur \"Mann\" oder \"Frau\"");
}
I've also tried
if (scan.next().equalsIgnoreCase("Frau") || scan.next().equalsIgnoreCase("Mann")) {
System.out.println("Oh eine Dame des" + sClanname + " Clans");
sGender = scan.next();
} else {
System.out.println("Entschuldige, wir sind hier sehr konservativ...Es gibt nur \"Mann\" oder \"Frau\"");
}
and
if (!scan.next().equalsIgnoreCase("Frau") && !scan.next().equalsIgnoreCase("Mann")) {
System.out.println("Entschuldige, wir sind hier sehr konservativ...Es gibt nur \"Mann\" oder \"Frau\"");
}
Upvotes: 0
Views: 100
Reputation: 21
Each scan.next() is trying to scan next line from the input. If you wana compare the same input save it once in a variable and then use.
Upvotes: 1
Reputation: 4609
Compare sGender instead of scan.next
if (scan.next().equalsIgnoreCase("Frau")) {
System.out.println("Oh eine Dame des" + sClanname + " Clans");
sGender = scan.next();
} else if (sGender.equalsIgnoreCase("Mann")) {
System.out.println("Oh ein Herr des" + sClanname + " Clans");
sGender = scan.next();
} else {
System.out.println("Entschuldige, wir sind hier sehr konservativ...Es gibt nur \"Mann\" oder \"Frau\"");
}
Upvotes: 0
Reputation: 394126
You are not comparing the same String
in your if
and else if
statements, since each call to scan.next()
returns a new String
read from the input.
You should store the String in a variable and re-use it:
String s = scan.next();
if (s.equalsIgnoreCase("Frau")) {
System.out.println("Oh eine Dame des" + sClanname + " Clans");
sGender = scan.next();
} else if (s.equalsIgnoreCase("Mann")) {
Upvotes: 6