You are here: Home // Programming // Reading user input in java

Reading user input in java

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.

  1. package com.tctalk.myapp.java.src;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. /**
  8. * ReaduserInput.java – [This code reads the user input data from command prompt]
  9. *
  10. * @author TechCuBeTalk.com
  11. * @version 1.0
  12. */
  13. public class ReaduserInput {
  14.  
  15.     public static void main(String[] args) {
  16.  
  17.         //  Ask the user to enter their name
  18.         System.out.print(“Please enter your name: “);
  19.  
  20.         //  To read user input create a reader object
  21.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  22.  
  23.         String userName = null;
  24.  
  25.         //  read the user input from command prompt by readLine() method
  26.         try {
  27.             userName = br.readLine();
  28.         } catch (IOException ioExcption) {
  29.             System.out.println(“IO exception occurred!”);
  30.             System.exit(1);
  31.         }
  32.  
  33.         System.out.println(“Welcome “ + userName + “!! Have a Good day!!”);
  34.  
  35.     }
  36.  
  37. }
  38.  
Blog Widget by LinkWithin

Related Articles:

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

Tags: , , , ,

Leave a Reply

Spam Protection by WP-SpamFree

Copyright © 2009 TechCuBeTalk free technology magazine. All rights reserved.
Designed by Theme Junkie. Powered by WordPress.