571df7f0ba65 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

ICT9- Java Programming

Java User Input (Scanner)


The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the 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:
Example:

import java.util.Scanner; // Import the Scanner class

public class Input{


public static void main(String[] args) {
Scanner MyObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");

String username;
username = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
}
}

Input Types
In the example above, we used the nextLine() method, which is used to read Strings. 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 an int value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
nextLine() Reads a String value from the user

In the example below, we use different methods to read data of various types:
Example
OUTPUT/EXECUTION
import java.util.Scanner;
public class Input_Dif_DataTypes {
public static void main(String[] args) throws
Exception {
Scanner myObj = new Scanner(System.in);
// String input
System.out.println("Enter name:");
String name = myObj.nextLine();
// Numerical input
System.out.println("Enter age:");
byte age = myObj.nextByte();
System.out.println("Enter salary:");
double salary = myObj.nextDouble();
System.out.println("Enter No of views:");
short views = myObj.nextShort();
System.out.print("Enter Meeting ID:");
int meetingId = myObj.nextInt();
System.out.print("Enter No of Covid cases:");
long cases = myObj.nextLong();
System.out.print("Enter height in cm:");
float H = myObj.nextFloat();
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("views: " + views);
System.out.println("meetingId: " + meetingId);
System.out.println("cases: " + cases);
System.out.println("Height: " + H);
}}

Scanner Class in Java


Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc.
and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an
input method for scenarios where time is a constraint like in competitive programming.
• To create an object of Scanner class, we usually pass the predefined object System.in, which
represents the standard input stream. We may pass an object of class File if we want to read
input from a file.
• To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example,
to read a value of type short, we can use nextShort()
• To read strings, we use nextLine().
• To read a single character, we use next().charAt(0). next() function returns the next token/word
in the input as a string and charAt(0) function returns the first character in that string.

Let us look at the code snippet to read data of various data types.

// Java program to read data of various types using Scanner class.


import java.util.Scanner;
public class ScannerInput {
public static void main(String[] args) throws Exception {
// predefined standard input object
Scanner sc = new Scanner(System.in);
// String input
System.out.print("Input Name: ");
String name = sc.nextLine();
// Character input
System.out.print("Input Gender: (M/F) ");
char gender = sc.next().charAt(0);
// Numerical data input
System.out.println("Input Age: ");
int age = sc.nextInt();
System.out.println("Input Mobile Number: ");
long mobileNo = sc.nextLong();
System.out.println("Input Temperature: ");
double temp = sc.nextDouble();
// Print the values to check if the input was correctly obtained.
System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Age: "+age);
System.out.println("Mobile Number: "+mobileNo);
System.out.println("Temperature: "+temp);
} }

You might also like