Exception in Java

Discussion in 'Programmer's Corner' started by arman19940326, Aug 4, 2016.

  1. arman19940326

    Thread Starter Member

    Jul 31, 2014
    37
    0
    Scrapbook_1470341177665.png

    Why is sc.next () necessary?
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    That depends on the logic that is being implemented, of which you have given no clue and are forcing us to guess.

    Is this a homework question?
     
    arman19940326 likes this.
  3. arman19940326

    Thread Starter Member

    Jul 31, 2014
    37
    0
    N
    Hi, No I'm reading a Java tutorial book and this is one of its examples. The book has provided these lines of reasons to answer my original question.
    "The next method must be called in the catch block to dispose of the user’s invalid input because the nextInt method leaves the input value in the Scanner’s input stream if an InputMismatchException is thrown. If you omit the statement that calls next, the while loop keeps reading it, throws an exception, and displays an error message in an infinite loop. If you don’t believe me, look at Figure 8-2. I found this error out the hard way. (The only way to make it stop is to close the console window.)"

    My problem is that when the next() method is called the program should wait for the user to enter something and then the next line can be executed but when I ran this code it seems that the line after the call of the next() method is run without me entering anything. I'm a bit confused.
     
  4. arman19940326

    Thread Starter Member

    Jul 31, 2014
    37
    0
    And this code tries to get an integer from the user and shows it in the console but when the user enters anything else Java compiler throws an exception. The purpose is to handle this exception by using try-catch blocks.
     
  5. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    Can you at least provide a link to the Scanner class definition so that we don't have to look it up for you?
     
    arman19940326 likes this.
  6. arman19940326

    Thread Starter Member

    Jul 31, 2014
    37
    0
  7. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    It is called without you entering anything (more) precisely because of the explanation the book gave you.

    When you executed the nextInt() method in the try block and it threw an exception (because you entered "Fred" instead of "42", for instance), the method threw the exception WITHOUT removing "Fred" from the input stream because nextInt() has no idea how you want to handle it. So the catch block calls next(), which simple gets the next token ("Fred") and moves on.

    The big problem with this code is that what if there isn't any input to begin with? If that's why nextInt() threw an exception, then the subsequent call to next() is going to throw the same exception which, at this point, will be uncaught.
     
    arman19940326 likes this.
  8. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    arman19940326 likes this.
  9. arman19940326

    Thread Starter Member

    Jul 31, 2014
    37
    0
    Thank you! I now have a better understanding. I didn't know about tokens and the fact that next method catches tokens. When user enters something and we want to get it by next method, in fact we created a token that can be caught by an appropriate Scanner class method. Am I right? I mean entering something as an input stream creates a token.
     
  10. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    I think your "big problem" is addressed by the class:

     
  11. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    @WBahn -- it appears that one purpose of the scanner class is ease data entry parsing for console based applications. But upon reading the class def, it seems quite useful for all kinds of tasks -- like parsing a text file of a particular known format. The fact that it supports regular expressions multiplies the usefulness. It's actually pretty neat. It reminds me of some stuff I just did recently in .asm...
     
    arman19940326 likes this.
  12. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    How is it addressed by that? The code that is presented appears (and it only appears because the desired behavior has not been provided) to want to simply skip input values that are not of the desired format. If there is no next token, then nextInt() will either block or throw a "NoSuchElementException" instead of the "InputMismatchException" that the catch block is intended to handle. Is the desired behavior that it will have an unhandled exception in that case? Or is the intent that the type of Scanner object created will block? I don't know, because we can only guess at what the desired behavior is.

    Another potential problem for the compiler is that it may not be discernible that the GetAnInteger() method will always actually return an integer (or throw an unhandled exception).
     
    arman19940326 likes this.
  13. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    It's also very useful for parsing String objects.

    There are a number of subtleties in its behavior, particularly where blocking versus nonblocking behavior is concerned since how you want it to behave when reading from a file is often quite different from how you want it to behave when reading from the console from a live operator (as opposed to a console stream that has simply been redirected to it from the output of another process).

    But you are correct, it is a very useful and powerful class.
     
  14. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    I think I see what you are getting at here, namely that under the two cases I initially referred to that nextInt() will throw two different kinds of exceptions and the exception handler only catches one of them. That is a very valid observation.
     
  15. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    I meant, there is an exception thrown for the particular case you mentioned. Not that the code example utilized it.

    I hope OP recognizes the value of studying the actual class definition -- not just the parts the book wants to use. When I come across a new concept while programming (or, anything else for that matter), I try to understand the full concept prior to proceeding. I may not need to know all at that moment, but I can't count how many times such info became invaluably important at some point in the future.
     
    arman19940326 likes this.
  16. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    I agree completely. It is not unusual for me to read the "real deal" from the standard or other authoritative source and for there to be a significant amount of it that sails over my head. But even when that is the case it usually gives me enough additional context to better understand the "for dummies" version and often gets me thinking in a direction that lets me find a better approach altogether.
     
  17. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    And while we are on the subject of concepts, let's be clear:

    The Java compiler produces byte code.
    The JVM interprets and executes the byte code and throws the exception.
     
  18. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    Just knowing the reference exists is a major advantage.
     
  19. arman19940326

    Thread Starter Member

    Jul 31, 2014
    37
    0
    This is the book I'm reading and I learn about Scanner class from this book.

    I can't have access to oracle.com. It seems the site doesn't allow IP addresses from Iran to proceed. I was too lazy to use VPN! Sorry...
     
    Last edited: Aug 5, 2016
Loading...