Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the user input in Java?

Tags:

java

input

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?

like image 444
Jtvd78 Avatar asked Mar 13 '11 04:03

Jtvd78


People also ask

How do I get user input in OOP?

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.

What are the 3 ways to input in Java?

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.

What does input () do in Java?

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.


1 Answers

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(); 
like image 73
Marco Aviles Avatar answered Oct 13 '22 15:10

Marco Aviles