Results 1 to 4 of 4

Thread: exception thrown for chardata = stdin.readline();

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    exception thrown for chardata = stdin.readline();

    Hello

    i was wondering how this piece of code executed without any problems

    import java.io.*;
    public class InputToDouble
    {
    public static void main (String[] args) throws IOException
    {
    String charData;
    double value;

    BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

    System.out.println("Enter a double:");

    charData = stdin.readLine();

    value = Double.parseDouble( charData ) ;
    System.out.println("value: " + value +" twice value: " + 2*value );
    }
    }


    whereas this piece of code

    import java.io.*;
    /**
    *
    * @author Moyeen
    */
    public class mywelcome {
    public static void main (String[] args)
    {
    String charData;
    double value;

    BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

    System.out.println("Enter a double:");

    charData = stdin.readLine();

    value = Double.parseDouble( charData ) ;
    System.out.println("value: " + value +" twice value: " + 2*value );
    }
    }

    gives the error
    mywelcome.java [21:1] unreported exception java.io.IOException; must be caught or declared to be thrown

    chardata = stdin.readline();

    why is the readline causing an exception?

  2. #2
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: exception thrown for chardata = stdin.readline();

    I dont know how the IOException is thrown, but all I know is that there could be an error while reading from the keyboard. I checked the Java API and it wasnt much help, but maybe some of the others can explain it better if you need it.

    But to fix that error, put a try...catch statement around that section, so it should look like this:
    Code:
    try{
         charData = stdin.readLine();
    
         value = Double.parseDouble( charData ) ;
    }
    catch(IOException i){
         //output some kind of message saying that there was a problem 
         //reading from the keyboard
    }
    catch(NumberFormatException n){     //this is thrown by Double.parseDouble()
        //output a message saying that it was incorrect format for the double
    }
    Java is very picky about exceptions, if there is a possibility that an exception is thrown, Java either makes you catch and deal with it, or you can just keep throwing it. Its always best to catch the exceptions and make meaningful error messages.

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: exception thrown for chardata = stdin.readline();

    This is how your throw a possible Exception in your code

    Code:
    /**
         * Read a line of text.  A line is considered to be terminated by any one
         * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
         * followed immediately by a linefeed.
         *
         * @return     A String containing the contents of the line, not including
         *             any line-termination characters, or null if the end of the
         *             stream has been reached
         *
         * @exception  IOException  If an I/O error occurs
         */
        public String readLine() throws IOException {
            return readLine(false);
        }
    And your code should look like:
    Code:
    public class mywelcome
    {
      public static void main (String[] args) {
        String charData ;
        double value = 0 ;
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)) ;
        System.out.println("Enter a double:") ;
        try {
          charData = stdin.readLine() ;
          value = Double.parseDouble(charData) ;
        }
        catch (IOException ex) {
        }
        System.out.println("value: " + value + " twice value: " + 2 * value) ;
      }
    }
    Last edited by ComputerJy; Jun 8th, 2006 at 02:20 AM.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: exception thrown for chardata = stdin.readline();

    Quote Originally Posted by fartman_900
    I checked the Java API and it wasn't much help, but maybe some of the others can explain it better if you need it.
    Java API explains better than any other source, a possible exception is thrown (any I/O Exception) since I/O Channels are most likely to fail.

    Quote Originally Posted by fartman_900
    Java is very picky about exceptions, if there is a possibility that an exception is thrown, Java either makes you catch and deal with it, or you can just keep throwing it. Its always best to catch the exceptions and make meaningful error messages.
    1- I'd call it cautious not picky, how many times your code crashed because you didn't handle possible exceptions in your code
    2- There are a best case scenario for all apps, so it's up to the developer when to handle exceptions
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width