How to Read User Input of Various Types in Java


In this example we will show you how to read user input of various types in Java.

Source Code

package com.beginner.examples;

import java.util.Scanner;

class ReadInputs {
  public static void main(String[] args) {
    // enter your input
    Scanner s = new Scanner(System.in);
    System.out.println("Enter your age and name:");

    //read your input    
    int a = s.nextInt(); // age
    String n = s.nextLine(); // name

    System.out.println("Age: " + a);
    System.out.println("Name: " + n); 

  }
}

Output:

Enter your age and name:
Age: 16
Name: Lucky

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments