Sometimes in our stand alone java program we need to read user input data. Below code snippet will show you how to read user input in a Java application.
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 SM Web Solutions * @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!!”); } }
Related Articles:
- JfreeChart Series-3: How to create a dynamic 3D Pie Chart using JFreeChart library in Java You may refer to the earlier tutorials if you have...
- Java Comparator example Sometimes we need to sort objects based on certain attributes...
- JfreeChart Series-2: How to create a dynamic Pie Chart using JFreeChart library in Java In the earlier tutorial I showed you how to create...
- How to create Windows executable EXE from a Java Jar Creating Windows executable EXE file from a Jar file: //...
- How to test if a String is palindrome or not in Java? Steps to check if a given string is a Palindrome...
- Step by step guide to create Singleton design pattern with Java Singleton design pattern is a kind of design pattern which...
- JfreeChart Series-1: How to create a dynamic Bar Chart using JFreeChart library in Java In Java creating any graph or chart like bar chart...
- How to load a properties file in Java This tutorial will explain how to access properties file through Java...
- File upload to BLOB field and display attachment dialog box Sometimes a developer needs to provide the feature in their...
- creating non duplicate array from duplicate array in Java This example helps you to generate a fresh and non...
You can subscribe by e-mail to receive news updates and breaking stories.