Question: import java.io . File; import java.io . FileNotFoundException; import java.util.Scanner; public class Main public class ReadRecord { public static void main ( String [ ]

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
public class ReadRecord {
public static void main(String[] args){
try {
// Open the file
File file = new File("address_records.txt");
Scanner scanner = new Scanner(file);
// Read and process each record
while (scanner.hasNextLine()){
// Read a line from the file
String line = scanner.nextLine();
// Check if the line starts with "Name: "
if (line.startsWith("Name: ")){
// Extract the name from the line
Name: Mario Mario;
Phone Number: 3333333333;
Address: Mushroom Kingdom;
Phone Number: 1111111111;
Address: Sesame Street;
Name: Santa Clause;
Phone Number: 7777777777;
Address: North Pole;
Name: Dracula;
Phone Number: 0000000000;
Address: Transylvania;
Name: Sherlock Holmes;
Phone Number: 1123581321;
Address: 221B Baker Street;
String name = line.substring(6);
// Print the name to the console
System.out.println(name);
}
}
// Close the scanner
scanner.close();
} catch (FileNotFoundException e){
// Print an error message if the file is not found
System.out.println("File not found.");
}
}
};
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!