Home java Check the entered data BufferedReader

Check the entered data BufferedReader

Author

Date

Category

began to study Java. Receded before entering the console. There are two ways, well, or I found only two: scanner and buferrider.
The scanner seemed to figure out, there you can check the data entered using the methods of Hasnext … ()
For example, that Inta can be entered so to check:

scanner sc = new scanner (System.in);
    if (SC.HASNEXTINT ())
      INT Number = sc.nextint ();
    ELSE.
      System.out.PrintLN ("not number.");

Only there instead of sout you need to “throw exceptions”, but I haven’t figured it out yet.

And what to do with buferrider? How to check it? Read something about “handle exceptions” did not quite understand how already wrote above
Maybe someone can write the correct code on the example, for example, to enter your intention. I understand that you need to check for not emptiness and int?


Answer 1

If the numbers are written each in their row, you can use this method

bufferedreader br = new buffereder (new inputstreamReader (System.in));
int i;
Try.
{
 i = integer.paraseint (br.Readline ());
}
Catch (NumberFormateException E)
{
 System.out.PrintLN ("NumberFormateException");
}

The ReadLine method returns the string entered in the console, and the PARSEINT method will return the number if the string is transmitted as a parameter corresponds to the number format. Otherwise, he will cause the NumberFormateException exception. But this method is quite capricious, even a space at the beginning of the line will prevent the number.


Answer 2

Java has 3 methods Reading input data from console :

  • Using BufferedReader class;
  • Using Scanner class;
  • Using Console class.

Difference You can read here and here .

code with examples of use All ways:

import java.io.bufferedReader;
Import java.io.console;
Import java.io.ioException;
Import java.io.InputStreamReader;
Import java.util.scanner;
Public Class ConsoleInPuteExamples.
{
  Public Static Void Main (String [] Args)
  {
   usingConsoleReader ();
   usingbufferedReader ();
   UsingScanner ();
  }
  Private Static Void UsingConsoleReader ()
  {
   Console Console = NULL;
   String InputString = NULL;
   Try.
   {
     // Create Console object
     Console = System.console ();
     // If Console is not equal to NULL
     If (Console! = NULL)
     {
      // Read the string from user input
      InputString = Console.ReadLine ("Name:");
      // Row output
      System.out.printLN ("Name Entered:" + InputString);
     }
   } Catch (Exception EX)
   {
     EX.PrintStackTrace ();
   }
  }
  Private Static Void UsingBufferedReader ()
  {
   System.out.PrintLN ("Name:");
   Try {
     BufferedReader BufferRead = 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 BufferedReader and the Scanner class is that the first class is only for reading string or text data, while the Scanner class is designed for both reading and for analyzing text data in primitive Java types, such as int, Short, Float, Double and Long.

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

So BufferedReader does not provide direct methods for reading Integer by the user entered. You can use the readline () method, however, initially you will have to read Integer in String format.

In the case of the PARSEINT () method, it takes a string value, parsing it as a decimal integer and returns.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions