Instructions Use the Java API htt docs oraclecom avase8docsa

Instructions Use the Java API (htt docs oracle.com avase/8/docs/a to answer the questions below

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.

 Instructions Use the Java API (htt docs oracle.com avase/8/docs/a to answer the questions below SolutionHi, Please find the answers below: The interface which

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site