ZMedia Purwodadi

How to take user input in java | technical-arbaab

Table of Contents

 How to take user input in java

Image Source - Google | Image by - besanttechnologies.com

It is so normal in a program when it's time to ask a user give some value in order to proceed in a program so let's see what are the methods to take input in java, Although there are four to five methods to take input in java but here we will talk about the most used one's -

  • Using Scanner class
  • Using BufferedReader class

Using Scanner class-Without any further delay let's move to the point in order to take input from the user you need to follow these steps sequentially-

1. Import the Scanner class from java.util package by writing on the top of the program import java.util.Scanner remember it is a case sensitive language so write command as it is exactly written here.

2. After importing Scanner class create the object of Scanner class in main method by writing this Scanner sc = new Scanner(System.in) here system.in is a low level standard input stream which means to take input from the keyboard as system.input(in).

3. After creating the object of scanner class you need to tell what type of input you want to take by writing this line like if you want to take integer input you will write int number(variable name) = sc.nextInt() similarly-

  • for double     double myDouble = sc.nextDouble()
  • for string       String myString = sc.nextLine()
  • for long         long myLongsc.nextLong()
  • for short        short myShortsc.nextShort()
  • for float         float myFloat = sc.nextFloat()
  • for byte         byte myByte = sc.nextbyte()
  • for  char       char myChar = sc.next().charAt(0)        

here, myDouble, myString, myLong, myShort ......etc are variable names you can choose according to you do not confuse in this. Without any more theory let's see directly in the code how to take input from scanner class-

 import java.util.Scanner;  
 public class UsingScanner {  
      public static void main(String[] args)  
      {  
           String name = null;  //string variable initiallization
           Scanner sc = new Scanner(System.in);  //creating the object of scanner class
           System.out.println("enter a number");  //prints something on the console
           int number = sc.nextInt();  //In this line actually input is taking from the user
           System.out.println("the number is "+number);  
           System.out.println();                //new line  
           String random = sc.nextLine();  
           System.out.println("enter your name");  
            name = sc.nextLine();  
            System.out.println("the name you entered is "+name);  
            System.out.println();  
            System.out.println("enter a short value");  
           short name2 = sc.nextShort();  //short type input is taking from the user
           System.out.println("the short you entered is"+name2);  
           System.out.println();  //for a gap of one line
           System.out.println("enter a character value");  
           char letter = sc.next().charAt(0);  
           System.out.println("the character you entered is "+letter);  
      }  
 }  

Output

 enter a number  
 45  
 the number is 45  
 enter your name  
 Arbaab  
 the name you entered is Arbaab  
 enter a short value  
 2  
 the short you entered is2  
 enter a character value  
 jack  
 the character you entered is j  

Using BufferedReader Class-

It is also one of the method to take input from the user and moreover it is the more recommended method in-place of scanner because of it's advantages which we will discuss at the end of this article so let's see how it is used in java to take input, In order to take input from this method follow these steps sequentially-

1. Import the BufferedReader class, InputStreamReader class and IOException class or you can use import java.io.* directly if you want to import all the things which are inside io in one line.

2. After importing all these things create an object of BufferedReader class and inside in it pass the object of InputStreamReader class and inside in it pass System.in.

3. Now take the desired input according to the user as given below:

  • for String input      String name=b.readLine()
  • for integer input    int number=Integer.parseInt(b.readLine())
  • for float input        float myFloat=Float.parseFloat(b.readLine())
  • for byte input        byte myByte=Byte.parseByte(b.readLine())
  • for long input       long myLong=Long.parseLong(b.readLine())
  • for short input     short myShort=Short.parseShort(b.readLine())
  • for double input  double myDouble=Double.parseDouble(b.readLine())
  • for char input char c = (char) b.read()

So without any more theory go straight to the code to practically understand " how to take
different types of user input by using BufferedReader class"-

 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  
 public class UsingBufferedReader {  
      public static void main(String[] args) throws IOException  
      {  
           BufferedReader b =new BufferedReader(new InputStreamReader(System.in));  
           System.out.println("enter your name");  
           String name = b.readLine();  
           System.out.println();  
           System.out.println("enter a integer value");  
           int number1 = Integer.parseInt(b.readLine());  
           System.out.println("enter a double value");  
           double myDouble = Double.parseDouble(b.readLine());  
           System.out.println();  
           System.out.println("enter a character value");  
           char myChar = (char) b.read();  
           System.out.println("the character value you entered is "+myChar);  
      }  
 }  

Output:

 enter your name  
 Arbaab  
 enter a integer value  
 54  
 enter a double value  
 6767676777777  
 enter a character value  
 Martha  
 the character value you entered is M  

Note:  In the last value as you see in place of single character if the user has given complete string it does not has any effect it will take only the 0th index element as character as we write above charAt[0].

Important Note:

One important thing in BufferedReader method to note here is that it takes input as a sequence of characters so you can take string input easily by bufferedReaderObject.readLine() but if you want to take input other than string you have to type cast it to the desired data-type suppose integer input you want to take so you have to write like this Integer.ParseInt(bufferedReaderObject.readLine()) and there is one more different case of character input, for this you have to write char c = (char) b.read(); here read method takes value as a string so you have to type cast it manually into char as shown above and don't forget to declare throws IO Exception as shown in the picture . It has to be declared in BufferedReader input because there chances of file deletion or viruses in the file sometimes BufferedReader takes data from a network stream where the reading system can fail at any time So this type of error can occur in input operation when a BufferedReader is used. This is why a buffered reader throws IOException.
































Arbaab khan
Arbaab khan FEAR has two meanings "forget everything and run" "Face everything and rise" The choice is yours Welcome u in our tech blog

إرسال تعليق