Can you please help me answer this java problem Also if you

Can you please help me answer this java problem. Also if you can, please explain it

Consider the following code snippet that reads a text file where there
are 10 lines of data, and where each line has 10 items of numerical data
separated by space. The file is read using a Scanner object named scan:
int count=0;
while(scan.hasNextLine())
{
String data=scan.nextLine();
count++;
}
Choose the correct statement below:
a) The value of count after the loop is done iterating is 0
b) The value of count after the loop is done iterating is 10
c) The value of count after the loop is done iterating is 100
d) The value of count can not be determined because the following line of
code is missing within the loop: int someint=scan.next();
e) The value of count after the loop is done iterating is determined via the
code:
int c=scan.getCount;

Solution

b) The value of count after the loop is done iterating is 10

int count=0;
while(scan.hasNextLine())
{
String data=scan.nextLine();
count++;
}

The nextLine() method of Scanner class reads one line at a time from file. this method will not recognise integers in a line but the whole line as string. It then increments the value of count for each line read in the while loop . So for 10 lines in the file , the count will be incremented to 10.

So option a,c,d and e are incorrect.

Can you please help me answer this java problem. Also if you can, please explain it Consider the following code snippet that reads a text file where there are 1

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site