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.

4
  • Why don't you print out the content of mascotQuestion1 and see if it is what you expect? Commented Sep 30, 2014 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 + "'."); Commented Sep 30, 2014 at 23:22
  • try mascotQuestion1 = scan.next().trim(); to see if it helps. Commented Sep 30, 2014 at 23:24
  • Note: University of Michigan's mascot is the wolverine. University of Wisconsin is the badger. I'd hate for you to get marked down on this question for having the wrong answer. Commented Oct 1, 2014 at 0:57

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm under certain restrictions because I'm a student, so I can't do it that way.
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
1

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

1 Comment

This doesn't work...I tried this already and it messes with the output in a strange way
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--;
  }

2 Comments

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?
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)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.