Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java calculator randomly outputted line

Tags:

java

So I've just started java with a tiny bit of experience from a few other languages. I tried to make this basic calculator and had a lot of problems but managed to resolve most of them. The last thing that I can't seem to understand is a randomly triggered "This is an invalid input", every time my program runs once. "..." refers to irrelevant code. Everything else seems to work fine. Thanks in advance!

import java.util.Scanner;
public class Calc {
...
        System.out.println("Would you like to use the calculator?(Y/N)");
        while(use){
            String usage=in.nextLine().toLowerCase();
            if(usage.equals("n")){use=false;}
        //input
            //operations
            else if(usage.equals("y")){
                ...(calculator code)
            System.out.println("Continue use? (Y/N)");
            }
            else {System.out.println("That is not a valid input");}
        }
    }
}

After running my code a few times, my output is

Would you like to use the calculator?(Y/N)
Y
Please input an operation: +,-,*,/,%, ^, or root
+
Calculator: Please input your first number.
1
Now enter your second number.
2
Calculating
3.0
Continue use? (Y/N)
That is not a valid input  <-- right there is the confusing part, why is that triggered?
Y
Please input an operation: +,-,*,/,%, ^, or root

Full code is on pastebin, if you somehow need it. http://pastebin.com/Qee2Hxe3

like image 560
Joel Ye Avatar asked Aug 25 '26 09:08

Joel Ye


2 Answers

I checked the full code, and right before the loop first reiterates, there is a call to in.nextDouble(), this method reads a double but does not consume the line end, which makes the next in.readLine() return \n immidiately and the succeeding test fails.

A simple solution is to manually consume the line-end:

System.out.println(ans);
System.out.println("Continue use? (Y/N)");
in.nextLine();
like image 99
Ahmed KRAIEM Avatar answered Aug 26 '26 23:08

Ahmed KRAIEM


I tested your code and found that a solution is to declare your scanner inside your while loop, like so:

while (use) {
            Scanner in = new Scanner(System.in);
            String usage = in.nextLine().toLowerCase();

Here's what the problem is: first, you are entering your while loop, and usage is set equal to in.nextLine(). Since there is no next line, it waits for you to enter one. You enter yes, after which you enter your formula. Then it returns the answer, and goes back to the top of the while loop. Once again, usage is set to equal in.nextLine, but there is already a next line (a blank one) and so usage is set to equal an empty String ("") which is neither "y" or "n". Then it immediately goes to the "else" option at the end and prints the "invalid" message.
Re-assigning your scanner through each iteration of your while loop fixes this problem.

like image 23
James Dunn Avatar answered Aug 26 '26 22:08

James Dunn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!