Checking the input data BufferedReader

I started studying Java. I got to the input from the console. There are two ways, or I found only two: a scanner and a bufferrider. I sort of figured out the scanner, there you can check the input data using the hasNext...() methods, for example, you can check that int is entered like this:

Scanner sc = new Scanner(System.in);
       if (sc.hasNextInt()) 
           int number = sc.nextInt();
       else 
           System.out.println("Не число.");

Only there, instead of sout, you need to "throw exceptions", but I haven't figured this out yet.

And what to do with the bufferrider? How to check it? I read something about "handle exceptions". I completely understood, as I wrote above Maybe someone can use an example to write the correct code, for example, to enter an int value. I understand that you need to check for non-emptiness and the type of int?

Author: Popov Egor, 2020-02-08

2 answers

If the numbers are written each in its own line, then you can use this method

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i;
try
{
  i = Integer.parseInt(br.readLine());
}
catch(NumberFormatException e)
{
  System.out.println("NumberFormatException");
}

The readLine method returns the string entered in the console, and the parseInt method returns a number if the string passed as a parameter matches the number format. Otherwise, it will throw a NumberFormatException. But this method is quite capricious, even a space at the beginning of the line will prevent you from getting a number.

 0
Author: Gleb, 2020-02-08 03:50:53

In Java, there are 3 ways to read input data from the console:

  • using the Bufferedreader class;
  • using the Scanner class;
  • using the Console class.

The difference can be read here and here.

Code with examples of using all methods:

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class ConsoleInputExamples
{
   public static void main(String[] args)
   {
      usingConsoleReader();
      usingBufferedReader();
      usingScanner();
   }

   private static void usingConsoleReader()
   {
      Console console = null;
      String inputString = null;
      try
      {
         // cоздать объект console
         console = System.console();
         // если console не равен null
         if (console != null)
         {
            // прочитать строку из пользовательского ввода
            inputString = console.readLine("Name: ");
            // вывод строки
            System.out.println("Name entered : " + inputString);
         }
      } catch (Exception ex)
      {
         ex.printStackTrace();
      }
   }

   private static void usingBufferedReader()
   {
      System.out.println("Name: ");
      try{
         BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
         String inputString = bufferRead.readLine();

         System.out.println("Name entered : " + inputString);
     }
     catch(IOException ex)
     {
        ex.printStackTrace();
     }
   }

   private static void usingScanner()
   {
      System.out.println("Name: ");

      Scanner scanIn = new Scanner(System.in);
      String inputString = scanIn.nextLine();

      scanIn.close();            
      System.out.println("Name entered : " + inputString);
   }   
}

One of the main differences between the BufferedReader and the Scanner class is that the first class is intended only for the for reading string or text data, whereas the Scanner class is designed for both reading and analyzing text data in primitive Java types such as int, short, float, double, and long.

BufferedReader can only read String, and Scanner can read both String and other data types, such as int, float, long, double, float, etc.

Thus, the BufferedReader does not directly provide methods for reading the integer entered by the user. You can use the readLine () method, but initially you will have to read an integer in the String format.

In the case of the parseInt () method, it takes a String value, parses it as a decimal integer, and returns.

 0
Author: invzbl3, 2020-02-08 12:21:48