!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Java!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ID: 3603048 • 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
CODE FOR THE ABOVE PROBLEM:---
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class MyScanner {
private String inputString;
String[] splitedString = null;
String[] CopyString = null;
public MyScanner(String inputString) {
super();
this.inputString = inputString;
this.splitedString = inputString.split(" ");
}
public String next() {
return getInputString();
}
private String getInputString() {
String stringTofetch = null;
try{
stringTofetch = splitedString[0];
}catch (Exception e) {
throw new NoSuchElementException();
}
String[] shortenedArray = new String[splitedString.length-1];
for(int i =1;i<splitedString.length;i++) {
shortenedArray[i-1] = splitedString[i];
}
splitedString = shortenedArray;
return stringTofetch;
}
public boolean hasNext() {
boolean returnFlag = false;
CopyString = (null==CopyString) ?splitedString : CopyString;
String[] shortenedArray = new String[CopyString.length>0?CopyString.length-1:0];
for(int i =1;i<CopyString.length;i++) {
shortenedArray[i-1] = CopyString[i];
}
CopyString = shortenedArray;
if(CopyString.length>0) {
returnFlag = true;
}
return returnFlag;
}
public int nextInt() {
try{
return Integer.parseInt(next());
} catch (Exception e) {
throw new InputMismatchException();
}
}
public boolean hasNextInt() {
boolean returnFlag = false;
CopyString = (null==CopyString) ?splitedString : CopyString;
String[] shortenedArray = new String[CopyString.length>0?CopyString.length-1:0];
for(int i =1;i<CopyString.length;i++) {
shortenedArray[i-1] = CopyString[i];
}
CopyString = shortenedArray;
try{
if(CopyString.length>0) {
Integer.parseInt(CopyString[0]);
returnFlag = true;
}
}catch (Exception e) {
returnFlag = false;
}
return returnFlag;
}
}
Hope this helps!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.