Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Java Tip

Thread: Reading int values using BufferedReader

  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    10

    Default Reading int values using BufferedReader

    The static method below reads only int values using BufferedReader (stdin) and throws Exception if int is not read.

    Java Code:
     public static int readInt () {
       while (true) {
         try{
           String line = stdin.readLine();
           int value = Integer.parseInt(line);
           return value;
         } catch(java.lang.NumberFormatException e){
           ;
         } catch(IOException e){
           ;
         }
       }
     } // end readInt ()
    ashishsinghdev likes this.

  2. #2
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

    Default

    Another way is using the Scanner class.

    int value = new Scanner(System.in).nextInt();

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Welcome to the Java Forums Christian!

    As you are probably aware, there are typically more than one way to perform a task in Java. :)

    Just wanted to welcome you to our community. See you around!
    -Capt
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Reading a file from Applet (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-22-2008, 11:51 PM
  2. Reading Float values from a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:20 AM
  3. Reading file contents (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 10:00 AM
  4. Reading text from a URL using BufferedReader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •