Saturday 17 December 2011

Getting user input in java

Hi all,
Execute the below given example. You will be prompted to enter the name.
Enter a name and press ENTER.

CODE:

import java.io.*;

public class getUserInput {
       public static void main(String[] args) {
              System.out.print("Enter your name: ");
              InputStreamReader istr = new InputStreamReader(System.in);
              BufferedReader br = new BufferedReader(istr);
               String userInput=null;
              try {
                     userInput = br.readLine();
              } catch (IOException e) {
                     e.printStackTrace();
              }
              if(userInput != null && userInput.trim().length()>0){
                     System.out.println("Hi! Welcome " + userInput);
              }else{
                     System.out.println("No data entered. ");
              }
             
       }
}





OutPut:

Enter your name: Donald
Hi! Welcome Donald

No comments:

Post a Comment