By following the below example of the code, you can read user input in a Java application:
Code:
package com.tctalk.myapp.java.src;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* ReaduserInput.java - [This code reads the user input data from command prompt]
*
* @author TechCuBeTalk.com
* @version 1.0
*/
public class ReaduserInput {
public static void main(String[] args) {
// Ask the user to enter their name
System.out.print("Please enter your name: ");
// To read user input create a reader object
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName = null;
// read the user input from command prompt by readLine() method
try {
userName = br.readLine();
} catch (IOException ioExcption) {
System.out.println("IO exception occurred!");
System.exit(1);
}
System.out.println("Welcome " + userName + "!! Have a Good day!!");
}
}
Bookmarks