Question: Assume file input.txt contains the text shown in the box below: Roses are red, Violets are blue, Sugar is sweet, But I have commitment issues.
Assume file input.txt contains the text shown in the box below:
Roses are red, Violets are blue, Sugar is sweet, But I have commitment issues. So I'd rather just be friends at this point in our relationship.
Trace the following program and write down the exact output as it would appear on the screen.
import java.util.Scanner; import java.io.*;
public class Test1 {
public static void main (String[] args)throws FileNotFoundException {
Scanner input = new Scanner(new File("input.txt"));
while (input.hasNextLine()) { String s = input.nextLine();
Scanner lineScan = new Scanner(s);
int y = 0;
while (lineScan.hasNext()) {
String x = lineScan.next();
y++;
}
System.out.println(y);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
