gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
java number input console
- User
java number input console
- Assistant
To read a number from the console in Java, you can use the
Scannerclass.Here's an example of reading an integer from the console:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); System.out.println("You entered: " + number); scanner.close(); } }In this example, we first create a
Scannerobject and passSystem.inas the input source, which represents the standard input (console). We then use thenextInt()method of theScannerclass to read an integer input from the user. Finally, we print the entered number to the console.Don't forget to call the
close()method of theScannerobject at the end to release any resources associated with it.