Console I/O

Console output using System.out

Console input using Scanner

Example

Scanner stdin = new Scanner(System.in);

System.out.print("Enter your name: ");
String name = stdin.nextLine();

System.out.print("Enter your age: ");
int age = stdin.nextInt();

System.out.println(name + " is " + age + " years old");

Something to consider...

Given the following code fragment:

Scanner stdin = new Scanner(System.in);

System.out.print("Enter your employee ID#: ");
int id = stdin.nextInt();

System.out.print("Enter your password: ");
// missing line

System.out.println("ID: " + id + " Password: " + password);

Consider the following two possibilities for // missing line

  1. String password = stdin.next();
  2. String password = stdin.nextLine();
Is there a difference in the output if possibility 1 is used to replace // missing line than if possibility 2 is used? If so, why?