Menu
Topics Index
...
`

Scanner2930


If you need explanation Read this topic

If you need Answer Take test on this topic

Scanner2930
What will be output of the following program?
import java.util.*;
public class ScannerExample {
    public static void main(String arg[]) {
        String i = "77777";
        Scanner sc = new Scanner(i);
        sc.useDelimiter("");
        while (sc.hasNext()) {
            if (sc.hasNextInt())
                System.out.print(sc.nextInt() + " ~ ");
        }
    }
}

A. 7 ~ 7 ~ 7 ~ 7 ~ 7 ~
B. 77777 ~
C. Compilation Error
D. Runtime Error
Topic: Java Scanner Class Methods With Examples

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

d.runtime error. due to infinite while loop..its condition is non terminating

Posted by Asma Mujtaba Khan    2014-08-25 04:12:18


A ANS.......

java.util.Scanner.useDelimiter(Pattern pattern)method sets this scanner's delimiting pattern to the specified pattern.

Thejava.util.Scanner.hasNext()method Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. Thescanner does not advance past any input.

hence when scanner find int it will print it...n the result is ans a

Posted by Nasir Khan    2014-08-25 04:16:45


D

NON TERMINATING CONDITIIN OF WHILE LOOP.
THE FINCTION hasnext()-->>
Thejava.util.Scanner.hasNext()method Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. Thescanner does not advance past any input.

this function causes the loop to repeat endlessly as terminating condition is not reached

Posted by Shamee Shamee    2014-08-25 04:20:06


Congratulations Nasir Khan. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-08-26 05:00:31


© meritcampus 2019

All Rights Reserved.

Open In App