165k views
1 vote
Download the needed files and look for the data.txt file. This file should have multiple lines, each line has first name, last name followed by multiple integers.

Create a class named MaximumOfEachLine with a main method.

Read the data from the "data.txt" file.

Find the maximum integer in each line.

-Display the first name and the last name followed by the maximum integer found in that line.

Make sure to have the proper import statements.

Your code must handle FileNotFoundException.

If the file is missing, your program should display "File Not Found".

If you did all the above correctly, this is the output you should get.

Samantha Johns

Max: 99

Michael Smith Max: 100.

John Michael

Max: 92

Mary Hernandez

Max: 92

George Johnson Max: 93-

Sara Anderson

Max: 83

Susan John

Mark Smith

Max: 84

Max: 69

-

User Jemina
by
8.1k points

1 Answer

3 votes

Final answer:

To find the maximum integers in each line of a file, you must create a Java class with a main method that reads the file, processes each line to find the maximum integer, and handles possible FileNotFoundException by displaying "File Not Found" message.

Step-by-step explanation:

Finding Maximum Integer in Each Line of a File

To achieve the task of finding the maximum integer in each line of a file and displaying it along with the first name and the last name, a Java class named MaximumOfEachLine is required. The class should have a main method that includes code to read data from a "data.txt" file. The following steps outline this process:






Here's an example of a simplified version of the necessary code structure:

import java.io.*;

class MaximumOfEachLine {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(" ");
// Assume first two elements are names
int max = Integer.MIN_VALUE;
for (int i = 2; i < data.length; i++) {
int value = Integer.parseInt(data[i]);
if (value > max) {
max = value;
}
}
System.out.println(data[0] + " " + data[1] + " Max: " + max);
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} catch (IOException e) {
e.printStackTrace();
}
}
}

Be sure to include error handling and close streams appropriately to avoid resource leaks.

User TrayMan
by
8.1k points