Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions