Reading user input in java

Posted on Jan 29, 2009 | Leave a Comment

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:

  1. 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...
  2. Java Comparator example Sometimes we need to sort objects based on certain attributes...
  3. 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...
  4. How to create Windows executable EXE from a Java Jar Creating Windows executable EXE file from a Jar file: //...
  5. How to test if a String is palindrome or not in Java? Steps to check if a given string is a Palindrome...
  6. Step by step guide to create Singleton design pattern with Java Singleton design pattern is a kind of design pattern which...
  7. 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...
  8. How to load a properties file in Java This tutorial will explain how to access properties file through Java...
  9. File upload to BLOB field and display attachment dialog box Sometimes a developer needs to provide the feature in their...
  10. creating non duplicate array from duplicate array in Java This example helps you to generate a fresh and non...

Leave a comment

Spam Protection by WP-SpamFree

Advertisement

Subscription

You can subscribe by e-mail to receive news updates and breaking stories.