How to Get Standard Input in Java


In this example, let’s us see how to get a simple input from the console.

Source Code

package com.beginner.examples;

import java.io.IOException;
import java.util.Scanner;

public class InputExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException
	{
		// TODO Auto-generated method stub
		//create Scanner Object 
		Scanner s=new Scanner(System.in);
		
		System.out.println("Please enter what you want:");
	
		String intputStr=s.nextLine();
		System.out.println("Your input is:"+intputStr);
	}

}

Output:

Please enter what you want:
I love you
Your input is:I love you

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments