Answered step by step
Verified Expert Solution
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.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."; ;
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
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started