Which exceptions can the next and nextFloat methods of the S

Which exceptions can the next and nextFloat methods of the Scanner class throw? (Hint: review the Java API documentation.) Are they checked exceptions or unchecked exceptions?

Solution

Scans the next token of the input as a float. This method will throw InputMismatchException if the next token cannot be translated into a valid float value as described below. If the translation is successful, the scanner advances past the input that matched.

If the next token matches the Float regular expression defined above then the token is converted into a float value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via Character.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to Float.parseFloat. If the token matches the localized NaN or infinity strings, then either \"Nan\" or \"Infinity\" is passed to Float.parseFloat as appropriate.

Returns:

the float scanned from the input

Throws:

InputMismatchException - if the next token does not match the Float regular expression, or is out of range

NoSuchElementException - if input is exhausted

IllegalStateException - if this scanner is closed

Example:

What is an Exception?

An Exception is an unwanted situation or event that occurs during the execution of a program, exceptions may lead to termination of the program if not handled properly. there are 2 types of exceptions

1) Unchecked Exception
2) Checked Exception

1) Unchecked Exception

The exceptions that are not checked at compile time are called unchecked exceptions, classes that extends RuntimeException comes under unchecked exceptions. Examples of some unchecked exceptions are listed below.

2) CheckedExceptions

Exceptions that are checked at compile-time are called checked exceptions, in Exception hierarchy all classes that extends Exception class except UncheckedException comes under checked exception category.

Which exceptions can the next and nextFloat methods of the Scanner class throw? (Hint: review the Java API documentation.) Are they checked exceptions or unchec

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site