Given a main program that searches for the ID or the name of a student from a text file, complete the findlID) and the findName) methods that return the corresponding information of a student. Then, insert a try/catch statement in main() to catch any exceptions thrown by findID() or findName(), and output the exception message. Each line in the text file contains the name and the ID of a student, separated by a space. Method findID0 takes two parameters, a student's name and a Scanner object containing the text file's contents. Method findID) returns the ID associated with the student's name if the name is in the file, otherwise the method throws an Exception object with the message 'Student ID not found for studentName", where studentName is the name of the student. Method findName) takes two parameters, a student's ID and a Scanner object containing the text file's contents. Method findName() returns the name associated with the student's ID if the ID is in the file, otherwise the method throws an Exception object with the message 'Student name not found for studentlD", where studentID is the ID of the student. The main program takes three inputs from a user: the name of a text file (String), a user choice of finding the ID or the name of a student (int), and the ID or the name of a student (String). If the user choice is 0, findID0 is invoked with the student's name as one of the arguments. If the user choice is 1, findName() is invoked with the student's ID as one of the arguments. The main program finally outputs the result of the search or a message if an exception is caught.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Needed code

1 import java.util.Scanner;
2 import java.io.FileInputStream;
3 import java.io.I0Exception;
4
5 public class LabProgram {
public static String findID(String studentName, Scanner infoScnr) throws Except
8
9.
/* Type your code here.
10
11
12
public static String findName(String studentID, Scanner infoScnr) throws Except
13
14
/* Type your code here. */
15
16 }
17 public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
String studentName;
String studentID;
String studentInfoFileName;
FileInputStream studentInfoStream = null;
Scanner studentInfoScanner = null;
18
19
20
21
22
23
24
// Read the text file name from user
studentInfoFileName = scnr.next();
25
26
27
28
// Open the text file
studentInfoStream = new FileInputStream(studentInfoFileName);
studentInfoScanner = new Scanner(studentInfoStream);
29
30
31
// Read search option from user. 0: findID(), 1: findName()
int userChoice = scnr.nextInt();
32
33
34
35
36
// FIXME: findID() and findName() may throw an Exception.
//
Insert a try/catch statement to catch the exception and output the
37
38
if (userChoice == 0) {
studentName = scnr.next();
studentID = findID(studentName, studentInfoScanner);
System.out.println(studentID);
}
else {
studentID = scnr.next();
studentName = findName(studentID, studentInfoScanner);
System.out.println(studentName);
}
studentInfoStream.close();
39
40
41
42
43
44
45
46
47
48
49
}
Transcribed Image Text:1 import java.util.Scanner; 2 import java.io.FileInputStream; 3 import java.io.I0Exception; 4 5 public class LabProgram { public static String findID(String studentName, Scanner infoScnr) throws Except 8 9. /* Type your code here. 10 11 12 public static String findName(String studentID, Scanner infoScnr) throws Except 13 14 /* Type your code here. */ 15 16 } 17 public static void main(String[] args) throws IOException { Scanner scnr = new Scanner(System.in); String studentName; String studentID; String studentInfoFileName; FileInputStream studentInfoStream = null; Scanner studentInfoScanner = null; 18 19 20 21 22 23 24 // Read the text file name from user studentInfoFileName = scnr.next(); 25 26 27 28 // Open the text file studentInfoStream = new FileInputStream(studentInfoFileName); studentInfoScanner = new Scanner(studentInfoStream); 29 30 31 // Read search option from user. 0: findID(), 1: findName() int userChoice = scnr.nextInt(); 32 33 34 35 36 // FIXME: findID() and findName() may throw an Exception. // Insert a try/catch statement to catch the exception and output the 37 38 if (userChoice == 0) { studentName = scnr.next(); studentID = findID(studentName, studentInfoScanner); System.out.println(studentID); } else { studentID = scnr.next(); studentName = findName(studentID, studentInfoScanner); System.out.println(studentName); } studentInfoStream.close(); 39 40 41 42 43 44 45 46 47 48 49 }
Given a main program that searches for the ID or the name of a student from a text file, complete the findID() and the
findName) methods that return the corresponding information of a student. Then, insert a try/catch statement in main) to
catch any exceptions thrown by findID() or findName(), and output the exception message. Each line in the text file contains
the name and the ID of a student, separated by a space.
Method findID0 takes two parameters, a student's name and a Scanner object containing the text file's contents. Method
findID) returns the ID associated with the student's name if the name is in the file, otherwise the method throws an
Exception object with the message "Student ID not found for studentName", where studentName is the name of the student.
Method findName) takes two parameters, a student's ID and a Scanner object containing the text file's contents. Method
findName) returns the name associated with the student's ID if the ID is in the file, otherwise the method throws an
Exception object with the message "Student name not found for studentID", where studentID is the ID of the student.
The main program takes three inputs from a user: the name of a text file (String), a user choice of finding the ID or the name
of a student (int), and the ID or the name of a student (String). If the user choice is 0, findID0 is invoked with the student's
name as one of the arguments. If the user choice is 1, findName() is invoked with the student's ID as one of the arguments.
The main program finally outputs the result of the search or a message if an exception is caught.
Ex If the input of the program is:
roster.txt 0 Reagan
and the contents of roster.txt are:
Reagan rebradshaw835
Ryley rbarber894
Peyton pstott885
Tyrese tmayo 945
Caius ccharlton329
the output of the program is:
rebradshaw835
Ex If the input of the program is:
roster.txt 0 Mcauley
the program outputs an exception message:
Student ID not found for Mcauley
Ex: If the input of the program is:
roster.txt 1 rebradshaw835
the output of the program is:
Reagan
Ex If the input of the program is:
roster.txt 1 mpreston272
the program outputs an exception message:
Student name not found for mpreston272
Transcribed Image Text:Given a main program that searches for the ID or the name of a student from a text file, complete the findID() and the findName) methods that return the corresponding information of a student. Then, insert a try/catch statement in main) to catch any exceptions thrown by findID() or findName(), and output the exception message. Each line in the text file contains the name and the ID of a student, separated by a space. Method findID0 takes two parameters, a student's name and a Scanner object containing the text file's contents. Method findID) returns the ID associated with the student's name if the name is in the file, otherwise the method throws an Exception object with the message "Student ID not found for studentName", where studentName is the name of the student. Method findName) takes two parameters, a student's ID and a Scanner object containing the text file's contents. Method findName) returns the name associated with the student's ID if the ID is in the file, otherwise the method throws an Exception object with the message "Student name not found for studentID", where studentID is the ID of the student. The main program takes three inputs from a user: the name of a text file (String), a user choice of finding the ID or the name of a student (int), and the ID or the name of a student (String). If the user choice is 0, findID0 is invoked with the student's name as one of the arguments. If the user choice is 1, findName() is invoked with the student's ID as one of the arguments. The main program finally outputs the result of the search or a message if an exception is caught. Ex If the input of the program is: roster.txt 0 Reagan and the contents of roster.txt are: Reagan rebradshaw835 Ryley rbarber894 Peyton pstott885 Tyrese tmayo 945 Caius ccharlton329 the output of the program is: rebradshaw835 Ex If the input of the program is: roster.txt 0 Mcauley the program outputs an exception message: Student ID not found for Mcauley Ex: If the input of the program is: roster.txt 1 rebradshaw835 the output of the program is: Reagan Ex If the input of the program is: roster.txt 1 mpreston272 the program outputs an exception message: Student name not found for mpreston272
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY