Instructions Use the Java API htt docs oraclecom avase8docsa
Solution
Hi,
Please find the answers below:
The interface which specifies that scanner should have hasnext() method:
Scanner implements the iterable<String> Interface. And because of this interface we can use hasNext() Method.
class ScannerWrapper implements Iterable<E> {
public Scanner scanner;
public ScannerWrapper(Scanner scanner) {
this.scanner = scanner;
}
public Iterator<String> iterator() {
return scanner;
}
}
Scanner scanner = new Scanner(\"one,two,three\");
scanner.useDelimiter(\",\");
ScannerWrapper wrapper = new ScannerWrapper(scanner);
for(String s : wrapper) {
System.out.println(s);
}
2. For nextline() also we are having Iterable<String> Interface.
Advances this scanner past the current line and returns the input that was skipped.
Scanner scanner = new Scanner(new File(\"tasks.txt\"));
List<String> listLines = new ArrayList<>();
while (scanner.hasNext()) {
String line = scanner.nextLine();
listLines.add(line);
}
System.out.println(listLines);
scanner.close();
3. Predefined classes like ArrayDeque and LinkedList implement the Deque interface.
