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?
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?
Use the
System
class to get the input.http://fresh2refresh.com/java-tutorial/java-input-output/ :
Just one extra detail. If you don't want to risk a memory/resource leak, you should close the scanner stream when you are finished:
Note that java 1.7 and later catch this as a compile warning (don't ask how I know that :-)
You can use any of the following options based on the requirements.
Scanner
classBufferedReader
andInputStreamReader
classesDataInputStream
classThe
readLine
method from theDataInputStream
class has been deprecated. To get String value, you should use the previous solution with BufferedReaderConsole
classApparently, this method does not work well in some IDEs.
You can use the Scanner class or the Console class
Here is how you can get the keyboard inputs: