I attempted to create a calculator, but I can not get it to work because I don't know how to get user input.
How can I get the user input in Java?
You need to use a Scanner in your main() class. For example in your code: Scanner input = new Scanner (System.in); needs to be entered before you try to get the user's input.
In the Java program, there are 3 ways we can read input from the user in the command line environment to get user input, Java BufferedReader Class, Java Scanner Class, and Console class.
Java input and output is an essential concept while working on java programming. It consists of elements such as input, output and stream. The input is the data that we give to the program. The output is the data what we receive from the program in the form of result.
One of the simplest ways is to use a Scanner
object as follows:
import java.util.Scanner; Scanner reader = new Scanner(System.in); // Reading from System.in System.out.println("Enter a number: "); int n = reader.nextInt(); // Scans the next token of the input as an int. //once finished reader.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With