Input in Java – Different Ways

java input

Here we will learn how to accept input in Java. Java has many different ways to accept user input. Here, we will learn many of them with an example.

1. Input Using Scanner

The Scanner is a predefined class of java.util package (Library), we need to import before using Scanner class. It has many different methods to access different types of data. Let’s start with the example.

Example: Accept input using Scanner class in Java.

//example to accept different types of values
import java.util.Scanner;
class InputExample
{
public static void main(String arg[])
{
	String pid;
	double price;
    int qty;
	//creating an object of Scanner
	Scanner in=new Scanner(System.in);

	//string input
	System.out.print("Enter Product's Id : ");
	pid = in.next();

	//integer input
	System.out.print("Enter Product's Quantity : ");
	qty = in.nextInt();

	//fractional input
	System.out.print("Enter Product's Price : ");
	price = in.nextDouble();
	
	System.out.println("Product id is "+pid+" and Price is "+price);
	System.out.println("You have ordered Total "+qty+" items");
}
}		 

2. Input Using Dialog Box

In Java, we can accept user input using the dialogue box also. The dialogue box is a small window with a textbox and a button. The JOptionPane.showInputDialog() is used to generate a dialog box, and we need to import a package javax.swing.JOptionPane for this. Let’s check the example.

Example: Input using dialog box in Java.

//User input using dialog box
import javax.swing.JOptionPane;
class InputExample
{
public static void main(String arg[])
{
	String pname;
	int qty;

	//input using dialog box
	pname=JOptionPane.showInputDialog("Enter Product's Name");
	qty=Integer.parseInt(JOptionPane.showInputDialog("Enter Product's Quantity"));

	System.out.println("You have ordered Total "+qty+" of "+pname);
}
}		 

In the above example, Integer.parseInt() is a method to convert string in integer. Input accepted as a string in Java, we need to typecast in other data type.

3. Input Using BufferedReader in Java

The BufferedReader class is used to accept input from the input stream as a String. It is a part of the java.io package, we need to import in our program. The readLine() method of the BufferedReader class is used to read a line from the console.

import java.io.*;
class InputExample
{
public static void main(String arg[]) throws IOException
{
	//creating object of BufferedReader
	BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	System.out.println("Enter Your Name");
    String name = reader.readLine();
	//readLine() method reads the line and store in a string variable
    System.out.println("Hello : "+name);
}
}		

4. Input using Command Line Arguments in Java

The command line means command prompt, from where we run our program. We can pass a value from command prompt also, and it stores in the argument of the main method, that is of an array type. We can pass multiple values, the first value will store on index 0, the second value will store on index 1 and so on. The argument of the main method is always String type, so you need to typecast in another type if required.

class InputExample
{
public static void main(String arg[])
{
	String pname;
	int qty;

	pname=arg[0];	//first string input
	qty=Integer.parseInt(arg[1]);	//second integer input

    System.out.println("You have ordered Total "+qty+" of "+pname);
}
}		 
Output:
javac Example.java
java InputExample Laptop 5
You have ordered Total 5 of Laptop

In Java, we need to be careful when we pass input to the java program. The wrong type of input will raise an exception.