Menu
Topics Index
...
`

Scanner3005


If you need explanation Read this topic

If you need Answer Take test on this topic

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

A. 7 ~ 6 ~ 5 ~ 4 ~
B. 7 ~  6 ~  5 ~   4 ~
C. Compilaton 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.

ANS IS A

Here the scanner class is used to split the string as per the delimeter given.

--Here, string is defined with comma and space after every number.

--object of scanner class is created and that String is passed as argument.After that delimeter used is comma * i.e. anything after comma is used as delimeter. Here space is used after comma so that is the delimeter.

--while loop is used to print the token by using next element() method.

Posted by Mânïshå Mùlchåndânï    2015-02-18 19:52:03


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Goutham Singarapu. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-19 04:19:12


© meritcampus 2019

All Rights Reserved.

Open In App