Java Program to Get Input from User

In this post we will learn how to get input from user in java.

Java User Input:

The Scanner class is used to get user input, and it is found in java.util package.

we use the scanner class, to create an object of the scanner class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//taking input from the user
import java.util.Scanner; // Import the Scanner class
class Input{
public static void main(String[]args)
Scanner sc=new Scanner(System.in); // create object of scanner class
System.out.println("Enter your name :");
String name =sc.nextLine(); //Read the user input
System.out.println("your name is ="+name);
}
}
//taking input from the user import java.util.Scanner; // Import the Scanner class class Input{ public static void main(String[]args) Scanner sc=new Scanner(System.in); // create object of scanner class System.out.println("Enter your name :"); String name =sc.nextLine(); //Read the user input System.out.println("your name is ="+name); } }
//taking input from the user
import java.util.Scanner;   // Import the Scanner class
class Input{
public static void main(String[]args)
Scanner sc=new Scanner(System.in);  // create object of scanner class
System.out.println("Enter your name :");
String name =sc.nextLine(); //Read the user input
System.out.println("your name is ="+name);
}
}

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter your name:
xyz
Your name is = xyz
Enter your name: xyz Your name is = xyz
Enter your name:
xyz
Your name is = xyz

Input Types :

In the above example , we used the nextLine() method, which is used to read Strings that is input by user. To read other types, look at the table below:

Method Description
nextBoolean() Reads a boolean value from the user.
nextByte() Reads a byte value from the user.
nextDouble() Reads a double value from the user.
nextFloat() Reads a float value from the user.
nextInt() Reads a int value from the user.
nextLine() Reads a String value from the user.
nextLong() Reads a long value from the user.
nextShort() Reads a short value from the user.

 

In this Example we use different methods to read data of various types like integer, float  etc.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name, age and salary:");
// String input
String name = sc.nextLine();
//integer input
int age =sc.nextInt();
// Numerical input
double salary = sc.nextDouble();
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter name, age and salary:"); // String input String name = sc.nextLine(); //integer input int age =sc.nextInt(); // Numerical input double salary = sc.nextDouble(); // Output input by user System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary); } }
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter name, age and salary:");

    // String input
    String name = sc.nextLine();
    
    //integer input
   int age =sc.nextInt();

    // Numerical input
    double salary = sc.nextDouble();

    // Output input by user
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("Salary: " + salary);
  }
}

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter name, age and salary:
abc
18
30000
Name : abc
Age: 18
Salary:30000
Enter name, age and salary: abc 18 30000 Name : abc Age: 18 Salary:30000
Enter name, age and salary:
abc
18
30000
Name : abc
Age: 18
Salary:30000

 

More java practice programs:

Addition of two numbers in java

Find the Greatest of the Two Numbers in Java

Java Program to Check Number is Even or Odd

Java Program to Check Leap Year

 

Leave a Comment

%d bloggers like this:
Python4U
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.