Results 1 to 5 of 5
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default Exception Handling

    This is probably a dumb question but,
    why does
    Java Code:
    int numerator = read.nextInt();//input = 20
    int denominator = read.nextInt();//input = 4
    int fraction = numerator/denominator;
    System.out.printf("Fraction: %d",fraction);
    not give an exception, but the following code does:
    Java Code:
    int numerator = read.nextInt();//input = 20
    int denominator = read.nextInt();//input = 4
    double fraction = (double) numerator/denominator;
    System.out.printf("Fraction: %d",fraction);

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,657
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by eLancaster View Post
    This is probably a dumb question but,
    why does [ ... ] not give an exception, but the following code does: [ ... ]
    It would've been nice if you'd told us what Exception was thrown ... possibly a stack trace, what exactly the input was etc. Please don't make us guess.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    %d is the placeholder for an int type, fraction is a double in your second example so you need to use %f

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Jos++

    In fact this one is so common (I mean "double" starts with a 'd'...) that Sun's JVM message uncharacteristically tells you what you should have written...

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by pbrockway2 View Post
    Jos++

    In fact this one is so common (I mean "double" starts with a 'd'...) that Sun's JVM message uncharacteristically tells you what you should have written...
    haha that is gas

    d != java.lang.Double

Similar Threads

  1. Exception Handling
    By liljester in forum New To Java
    Replies: 4
    Last Post: 06-21-2010, 03:09 PM
  2. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  3. JAXP exception handling
    By jovenky in forum Advanced Java
    Replies: 0
    Last Post: 05-27-2008, 01:37 PM
  4. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM
  5. JDBC - Exception handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 04:00 PM

Posting Permissions

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