Java Homework Background: The code for class ScanSupport given at the end of the
ID: 3587224 • Letter: J
Question
Java Homework
Background: The code for class ScanSupport given at the end of the notes for Lecture 10 on Sept. 26 raises some questions: Consider writing a nextFloat method to add to ScanSupport. A preliminary version of this would be very similar to nextName except that the final two lines would be:
a) ScanSupport has no tools equivalent to the hasNext methods of the scanner. If there is no next name on the current input line, what does nextName() return? (This has an impact on the pattern you use and it is the origin of an error in the above code.)
b) Give a declaration for the pattern named number above that recognizes 10, 9.12, 87. and .6345 (for example), as well as handling the possibility that there is no floating point number before the end of line.
c) Fix the code given above so that it returns Float.NaN if there is no next floating point number on the current input line. (That's a special float value that represents something that is not a number. If you have a float named f, f.isNaN() tests to see if the value is not a number.)
this is the code for class ScanSupport
** Support methods for scanning * see Errors class ScanSupport /* Pattern for identifers private static final Pattem name ** Pattern for whitespace excluding things like newline private static final Pattem whitespace ** Get next name without skipping to next line (unlike sc.Next)) Pattern. compile ( "[a-zA-70-9-K ) ; Pattern compile ( " [ ]x" ); @par sc the scanner from which end of line is scanned * @return the name, if there was one, or an empty string public static String nextNe ( Scanner sc ) { sc.skip( whitespace ): // the following is weird code, it skips the ne // and then retums the string that matched what was skipped sc. skip ( nane ) ; retun sc. match). group) /** Advance to next line and complain if is junk at the line end *see Errors * param message gives a prefix to give context to error messages @par sc te scanner from which end of line is scanned public static void lineEnd( Scanner sc, String message) I sc.skip( whitespace) String lineEnd-sc. nextLine () if (lineEnd. equals( "" )) & (lineEnd. startsWith "-*)) ) Errors.warn message + " followed unexpected by ,” + lineEnd + ”,”Explanation / Answer
a.
Returns the match result of the last scanning operation performed by this scanner. This method throws IllegalStateException if no match has been performed, or if the last match was not successful.
b.
Pattern float = Pattern.compile("([0-9]*(\.[0-9]*)?)|(\.[0-9]*)");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.