1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
eimsori [14]
3 years ago
13

- JAVA - Okay I have been attempting this for the past hour and I am really stuck and keep getting an error... The error says th

at I keep missing a main thread but its there... help!
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 findID() 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, findID() 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 tmayo945
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
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;

public class LabProgram {

public static String findID(String studentName, Scanner infoScnr) throws Exception {

/* Type your code here. */

}

public static String findName(String studentID, Scanner infoScnr) throws Exception {

/* Type your code here. */

}

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;

// Read the text file name from user
studentInfoFileName = scnr.next();

// Open the text file
studentInfoStream = new FileInputStream(studentInfoFileName);
studentInfoScanner = new Scanner(studentInfoStream);

// Read search option from user. 0: findID(), 1: findName()
int userChoice = scnr.nextInt();

// FIXME: findID() and findName() may throw an Exception.
// Insert a try/catch statement to catch the exception and output the exception message.

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();
}
}
Computers and Technology
1 answer:
swat323 years ago
7 0

Answer:

import java.util.Scanner;

import java.io.FileInputStream;

import java.io.IOException;

public class LabProgram {

 public static String findID(String studentName, Scanner infoScnr) throws Exception {

   while (infoScnr.hasNextLine()) {

     String[] parts = infoScnr.nextLine().split(" ");

     if (parts[0].trim().equals(studentName)) {

       return parts[1].trim();

     }

   }

   throw new Exception("Student ID not found for " + studentName);

 }

 public static String findName(String studentID, Scanner infoScnr) throws Exception {

   while (infoScnr.hasNextLine()) {

     String[] parts = infoScnr.nextLine().split(" ");

     if (parts[1].trim().equals(studentID)) {

       return parts[0].trim();

     }

   }

   throw new Exception("Student ID not found for " + studentID);

 }

 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;

   // Read the text file name from user

   studentInfoFileName = scnr.next();

   // Open the text file

   studentInfoStream = new FileInputStream(studentInfoFileName);

   studentInfoScanner = new Scanner(studentInfoStream);

   // Read search option from user. 0: findID(), 1: findName()

   int userChoice = scnr.nextInt();

   try {

     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);

     }

   } catch (Exception ex) {

     System.out.println(ex.getMessage());

   }

   scnr.close();

   studentInfoStream.close();

 }

}

Explanation:

- You should call your file the same as your class (i.e., LabProgram.java)

- I added a try/catch to both find calls, to catch the exception

- The find methods are very similar, they loop through the lines of the roster, splitting each line at a space and doing string comparison.

You might be interested in
________ a database rearranges data and objects in a database to make its size smaller
hodyreva [135]
<span>An attribute in a relation of a database that serves as the primary key of another relation in the same database is called a

</span>
8 0
3 years ago
Alan is quite surprised to see that his computer has been running slower than usual. Which of the following steps or actions sho
IgorLugansk [536]
3. I believe..........
3 0
4 years ago
Read 2 more answers
Three PCs are on an Ethernet LAN, connected by a hub. Describe how the Ethernet protocol deals with collisions in this situation
sdas [7]

Answer:

All the ports in a hub are in the same collision domain and a hub sends frames from one host to all other hosts in the network. This makes it prone to collision and poor network throughput. Just like a network switch, it uses the CSMA/CD protocol to detect collision in its network.

A network switch reduces its collision domain to just a port and sends frames from one host to another using its mac table as a route. This makes the network very efficient with high throughput. It also uses the CSMA/CD protocol to detect collision

Explanation:

Switches and hubs are used in networking to connect computer devices in a network. A hub is an obsolete device networking that broadcast a frame to all other ports or host in the network except for the send host port. This increases the rate of collision as all the ports in a hub share the same collision domain. A switch is an efficient frame switching device in a network, which uses its MAC table to decide and find a destination host.

CSMA/CD is a collision detection protocol used in a network to detect and prevent a collision. With this protocol, a host is able to listen, wait, send and resend frames to prevent a collision.

3 0
3 years ago
Guys, it took me 3 seconds to get banned on brainly, try to beat that, actually don't, unless you don't care about your account
Anarel [89]

How did u get banned?

Explanation:

6 0
3 years ago
Read 2 more answers
Taking a break is necessary while using your computer or any other gadget. What are ways of having a break? Give five examples
mina [271]

Answer:

1) is to stretch or do some workouts

2) is to read

3) is to play outside for an hour

4) is to do gardening

5) is rest

3 0
2 years ago
Read 2 more answers
Other questions:
  • Using a loop, write a program that reads in exactly five integers and outputs the sum.
    11·1 answer
  • Your company sent several staff members for UML training. An outside vendor provided the training. Everyone who attended the tra
    5·1 answer
  • The ___ are documents which uses http ?
    9·1 answer
  • What are new technologies, products, or services that eventually surpass the existing dominant technology or product in a market
    14·1 answer
  • Provide a few examples of how cryptography actually secures data.
    8·1 answer
  • Two types of formulas in Excel, what are they? A. Complex and simple, B. General and currency, C. Logical and Boolean, D. Trig a
    15·1 answer
  • 4.8 code practice question 2
    10·1 answer
  • RAM memory is intended?
    8·1 answer
  • Is ryzen really better for all-around purpose, and intel is better for gaming?
    7·1 answer
  • What are the supercomputers and where are they used? ​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!