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.ioFile;
import java.ioFileNotFoundException;
import java.util.Scanner;
public class Main
public class ReadRecord
public static void mainString args
try
Open the file
File file new Fileaddressrecords.txt;
Scanner scanner new Scannerfile;
Read and process each record
while scannerhasNextLine
Read a line from the file
String line scanner.nextLine;
Check if the line starts with "Name:
if linestartsWithName:
Extract the name from the line
Name: Mario Mario;
Phone Number: ;
Address: Mushroom Kingdom;
Phone Number: ;
Address: Sesame Street;
Name: Santa Clause;
Phone Number: ;
Address: North Pole;
Name: Dracula;
Phone Number: ;
Address: Transylvania;
Name: Sherlock Holmes;
Phone Number: ;
Address: B Baker Street;
String name line.substring;
Print the name to the console
System.out.printlnname;
Close the scanner
scanner.close;
catch FileNotFoundException e
Print an error message if the file is not found
System.out.printlnFile not found.";
;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
