Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Java!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

ID: 3602801 • Letter: #

Question

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Java!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Make Scanner class.

you are to design, implement, document, and test a class that provides objects that have a subset of the capabilities of Scanners

-Provide a constructor that takes a String as a parameter

-Provide four methods: <

1.hasNext(), which takes no parameters and returns a boolean

2.next(), which takes no parameters and returns a String

3.hasNextInt(), which takes no parameters and returns a boolean

4.nextInt(), which takes no parameters and returns an int

!!!!Not use Scanner in its implementation!!!!!

A MyScanner should behave just like a Scanner would if you only used the Scanner's String constructor and its hasNext(), next(), hasNextInt(), and nextInt() methods. Be sure that you understand exactly how Scanners behave, including the circumstances in which exceptions are thrown. Don't simply assume you know how those methods interact.

For example, hasNextInt() doesn't report whether there is an integer token somewherein the Scanner; it reports whether the next token can be parsed as an int by Integer.parseInt(). You should read the relevant parts of the Scanner documentation (Links to an external site.)Links to an external site. and (probably more importantly) experiment with Scanners.

Since there is no close() method, none of the MyScanner methods should ever throw an IllegalStateException. However, in the right circumstances some of the MyScanner methods should throw a java.util.NoSuchElement exception and/or a java.util.InputMismatchException.

Explanation / Answer

public Scanner(ReadableByteChannel source) {
this(makeReadable(source), WHITESPACE_PATTERN);
}

private static Readable makeReadable(ReadableByteChannel source) {
if (source == null)
throw new NullPointerException("source");
String defaultCharsetName =java.nio.charset.Charset.defaultCharset().name();
return Channels.newReader(source,java.nio.charset.Charset.defaultCharset().name());
}

1. public boolean hasNext() {
ensureOpen();
saveState();
while (!sourceClosed) {
if (hasTokenInBuffer())
return revertState(true);
readInput();
}
boolean result = hasTokenInBuffer();
return revertState(result);
}

2.

public boolean hasNext() {
ensureOpen();
saveState();
while (!sourceClosed) {
if (hasTokenInBuffer())
return revertState(true);
readInput();
}
boolean result = hasTokenInBuffer();
return revertState(result);
}

public String More ...next() {
ensureOpen();
clearCaches();

while (true) {
String token = getCompleteTokenInBuffer(null);
if (token != null) {
matchValid = true;
skipped = false;
return token;
}
if (needInput)
readInput();
else
throwFor();
}
}

3.   public boolean hasNextInt(int radix) {
setRadix(radix);
boolean result = hasNext(integerPattern());
if (result) { // Cache it
try {
String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
processIntegerToken(hasNextResult) :
hasNextResult;
typeCache = Integer.parseInt(s, radix);
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}

4. public int nextInt() {
return nextInt(defaultRadix);
}