Java Console Input
Access the Scanner code
import java.util.Scanner;
- Put this in the top area of the code between the package name class name.
- No need to understand this yet, it will be easier to understand it after you learn what a 'class' is.
Receive user input from the console
Scanner myInputReader = new Scanner (System.in);
String myTypedContent = myInputReader.nextLine();
- The code following the input commend will not be run until the user types into the console and presses the Enter key.
- Usually you should display a prompt (with System.out.println) before receiving user input, so that the user knows what to do.
- In this example, the variable 'myTypedContent' will store the value that the user types in.
Receive a specific type of input
int myTypedValue = myInputReader.nextInt();
- This also works for nextBoolean, nextFloat, nextDouble, nextLong, etc.
Challenge
Prompt the user for a number, then display that number multiplied by 5.
Quiz
Completed