0

I've looked around for a while and I couldn't find an exactly similar question. I'm writing a program that has a user choose what type of question they will be asked, then they input their answer. If they get it right, a point is added to their score. I can get it to work when the answers are incorrect, but not when they're correct. I know this is happening because the inputted strings do not match what the correct answers are stored as for some reason, but I cannot figure out why. Here's a section of the code:

System.out.println("What school do the Badgers belong to?");
mascotQuestion1 = scan.next();
if (mascotQuestion1.equalsIgnoreCase("University of Michigan")) {
    score++;
}
else if (mascotQuestion1.equalsIgnoreCase("don't know")) {
    score = (score + 0);
} 
else {
    score--;
}

Basically, the if and else if statements don't work. Every input is sent to the else statement. What's the problem?

EDIT: So, I tried printing the inputted mascotQuestion1 after entering "University of Michigan", and it came back "University". This is why it wrong, but I don't know how to fix this.

  • Why don't you print out the content of `mascotQuestion1` and see if it is what you expect? – khelwood Sep 30 '14 at 23:21
  • 1
    YOu really need to have some debug feedback for the fail case, like: `System.out.println("Expected answer 'University of Michigan' but got '" + mascotQuestion1 + "'.");` – rolfl Sep 30 '14 at 23:22
  • try mascotQuestion1 = scan.next().trim(); to see if it helps. – Dino Tw Sep 30 '14 at 23:24
  • Note: University of Michigan's mascot is the [wolverine](http://campusinfo.umich.edu/article/wolverine). University of Wisconsin is the [badger](https://en.wikipedia.org/wiki/Wisconsin_Badgers). I'd hate for you to get marked down on this question for having the wrong answer. –  Oct 01 '14 at 00:57

4 Answers4

1

If you are using the Java Scanner, you need to specify the delimiter pattern to use when looking at the tokens. I believe that it defaults to space, so when your user inputs his text, scanner.next() should retrieve the first whole word it finds.

Consider using BufferedReader, where you can use the reader.readLine() method.

Jay Harris
  • 4,201
  • 17
  • 21
Abbath
  • 582
  • 2
  • 7
  • I'm under certain restrictions because I'm a student, so I can't do it that way. – Michael Kavounis Sep 30 '14 at 23:32
  • Annother user suggested using the scan.nextLine() method. You may need to do some debugging if it is affecting your output, since that should work – Abbath Sep 30 '14 at 23:38
1

Try scan.nextLine(). I think scan.next() would only give you the next word.

Jay Harris
  • 4,201
  • 17
  • 21
Justin
  • 1,356
  • 2
  • 9
  • 16
0

First, you do not need the "if else" because score = (score + 0) does absolutely nothing

That greatly simplifies the code to:

if (mascotQuestion1.equalsIgnoreCase("University of Michigan")) {
    score++;
  } else { 
    score--;
  }
Thom Parkin
  • 346
  • 1
  • 9
  • I need some form of it. If they input "don't know" the score should stay the same, but if they input anything other than the correct answer or "don't know", the score is supposed to go down by one. If I don't have this statement, "don't know" would count as an incorrect answer and the score would go down...right? – Michael Kavounis Sep 30 '14 at 23:24
  • There is nothing significant about "don't know". It is simply and incorrect answer. It really depends upon your scoring requirements. Secondly, setting the score to `score + 0` accomplishes nothing. As a matter of fact the compiler will optimize that to a NOP; it will be completely ignored. I recommend you simply add a comment there (to yourself and any other future developers) – Thom Parkin Sep 30 '14 at 23:26
0

scan.next() will simply read the next word (more specifically a token) as it uses space as a delimiter by default. You could use scan.nextLine().

But it will be better to use a BufferedReader instead of Scanner as per your requirement. It will let you read a line at once using readLine().

Here's what you can do:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("What school do the Badgers belong to?");

mascotQuestion1 = br.readLine();     

if (mascotQuestion1.equalsIgnoreCase("University of Michigan")) {
    score++;
}
else if (mascotQuestion1.equalsIgnoreCase("don't know")) {
    score = (score + 0);
} 
else {
    score--;
}

Refer this post for more information on Scanner and BufferedReader Scanner vs. BufferedReader

Community
  • 1
  • 1
Kunjan Thadani
  • 1,660
  • 3
  • 18
  • 26