26.How to Read Input From Command Line in Java Using Scanner

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

26.How to read input from command line By Mr.

Vishnu

How to read input from command line in Java using Scanner

Scanner class is used to read input from command line. Scanner class is in java.util
package. Scanner class has methods like nextInt(), nextFloat(), nextDouble() etc which
read numeric data without any need of parsing using Integer.parseInt() etc. These
methods will throw an exception if you pass string or char type value.

Example:

Package com.sst;

import java.io.InputStreamReader;
import java.util.Scanner;

//This program is used to read input using Scanner Class.


public class ReadInputUsingScanner {
public static void main(String args[]) {
// create a scanner class object.
Scanner scanner = new Scanner(new InputStreamReader(System.in));
System.out.println("Enter your full name: ");

// read a line using scanner object.


String userName = scanner.nextLine();
System.out.println("Enter your full age: ");

// read an integer using scanner object.


int age = scanner.nextInt();

// print input values


System.out.println("User name : " + userName);
System.out.println("User age : " + age);
}

Output:

Enter your full name:


vishnu.G

1
Sri Sureka Technologies, NearAndraBank,OppositeGeethanjali High School, Near
S.R.NagarUmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-
9885602332.
26.How to read input from command line By Mr. Vishnu

Enter your full age:


26
User name :vishnu.G
User age : 26

2
Sri Sureka Technologies, NearAndraBank,OppositeGeethanjali High School, Near
S.R.NagarUmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-
9885602332.

You might also like