how to get input from user in java in Three Methods

We are providing you a java application that will communicate with the user at the command line and returns the user input. 


First Method :- 
 
 /*Here i write a program on Check Even/Odd from take input from Users Using Buffered Reader
Save these code in  AllEvenNum.java*/

import java.io.*;

class AllEvenNum{
  public static void main(String[] args) {
    try{
      BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter number : ");
      int num = Integer.parseInt(br1.readLine());
      System.out.println("Even Numbers:");
      for (int i=1;i <=num ; i++){
        if(i%2==0 ){
          System.out.print(i+",");
        }
      }
    }
    catch(Exception e){}
   
  }
}
 

Second Method :-
/* Here i write a program on Calculate total date of birth from take input from Users Using InputStreamReader
Save these code in  sawan.java*/
//It is better than above Program.

import java.io.*;
class sawan{
  public static void main(String[] args)
  {
    InputStreamReader istream = new InputStreamReader(System.in) ;
          BufferedReader bufRead = new BufferedReader(istream) ;
          System.out.println("Welcome To My First Java Program");
 
          try {
               System.out.println("Please Enter In Your First Name: ");
               String firstName = bufRead.readLine();

               System.out.println("Please Enter In The Year You Were Born: ");
               String bornYear = bufRead.readLine();

               System.out.println("Please Enter In The Current Year: ");
               String thisYear = bufRead.readLine();

               int bYear = Integer.parseInt(bornYear);
               int tYear = Integer.parseInt(thisYear);

              int age = tYear - bYear ;
               System.out.println("Hello " + firstName + " You are " +age+ "year old");
          }
         
          catch (IOException err) {}
            /*  System.out.println("Error reading line");
          }
          catch(NumberFormatException err) {
                           System.out.println("Error Converting Number");
          }   */
     }
}

Third and Last Method :-


We have prompt the user to enter the name by using System.out.print() method to keep the cursor on the same line. Then we have used the System.in object, along with the InputstreamReader and BufferedReader classes in order to read the user input. The br.readline() method reads the name from the command line. After pressing the enter key, you will get the user input.


/* Here i write a program on print  name from take input from Users Using Scanner
Save these code in  sawan.java*/

import java.io.*;
public class GetUserInput {
   
public static void main (String[] args) {
       
System.out.print("Enter your name and press Enter: ");
       
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
String name = null;
       
try {
         name
= br.readLine();
       
} catch (IOException e) {
         
System.out.println("Error!");
         
System.exit(1);
       
}
       
System.out.println("Your name is " + name);
}
}
0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment