5 Years
Discussion Span
Last Post by Fernando_1

The problem is here:

a= kb.nextDouble();
operator = kb.nextLine();
b = kb.nextDouble();

Getting the first nextDouble() reads up to the next delimiter (which means whitespace), but leaves that delimiter in the stream to be read again later. Then kb.nextLine() reads and returns everything up to the end of the line, which includes the delimiter. So operator must start with whitespace; there is no way to enter an operator without putting some whitespace between it and the first number; the first kb.nextDouble() will always throw an exception when you try.

Why not use next() instead of nextLine()? That way kb will take care of the whitespace for you.

Edited by bguild

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.