QUESTION IMAGE
Question
the following code is to read member, distance, and time from the file \workout.txt\, find the member with the greatest distance, and print the member and distance.
try{
scanner input = new scanner(new file(\workout.txt\));
string member = input.next();
int maxdis = / missing code a /;
double time = input.nextdouble();
while (input.hasnext())
{
string m = input.next();
int dis = input.nextint();
double t = / missing code b /;
if (maxdis < dis)
{
maxdis = dis;
member = m;
}
}
system.out.println(member + \ \ + time);
}
catch(ioexception e)
{
system.out.println(\couldnt open file\);
}
option 1:
a: input.next()
b: input.nextdouble()
option 2:
a: input.nextint()
b: input.nextdouble()
which is the correct option to replace the missing code?
- option 3
- option 1
- option 2
🆕 New Concept Discovered: File Parsing and Scanner Methods
Reading structured data sequentially using Scanner.
Step 1: Analyze the file structure and data types
The program reads data from a file named "workout.txt". Let's look at how the variables are declared and read initially:
String member = input.next();reads the first member's name (aString).int maxDis = / missing code A /;needs to read the first member's distance. SincemaxDisis declared as anint, we must use a method that returns an integer.double time = input.nextDouble();reads the first member's time (adouble).
This establishes that each record in the file consists of three parts in sequence:
Therefore, for missing code A, we need to read an integer:input.nextInt()
Step 2: Analyze the loop structure
Inside the while (input.hasNext()) loop, the program reads subsequent records in the same sequence:
String m = input.next();reads the next member's name.int dis = input.nextInt();reads the next member's distance.double t = / missing code B /;needs to read the next member's time. Sincetis declared as adouble, we must use a method that returns a double.
Therefore, for missing code B, we need to read a double:input.nextDouble()
Step 3: Match with the given options
Let's look at the options listed at the bottom left of the image:
- Option 1:
- A:
input.next() - B:
input.nextDouble() - Option 2:
- A:
input.nextInt() - B:
input.nextDouble()
Since missing code A must be input.nextInt() and missing code B must be input.nextDouble(), Option 2 is the correct choice.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
Option 2