User input (scanner) code example

Example 1: User input (scanner)

Scanner scanner = new Scanner(System.in);

System.out.println("What's your name?");
String name = scanner.nextLine();

System.out.println("What's your year of birth?");
int yearOfBirth = scanner.nextInt();

Example 2: User input (scanner)

String line = scanner.nextLine();
int age = Integer.parseInt(line); // Exception-t

Example 3: User input (scanner)

int age = scanner.nextInt();
scanner.nextLine(); //Ez fogja az ottmaradt sorvége jelet beolvasni
        
String name = scanner.nextLine();

Tags:

Java Example