Problem 5A (50 points) JAVA Data format We refer to crew members by their ID num
ID: 3711373 • Letter: P
Question
Problem 5A (50 points) JAVA
Data format
We refer to crew members by their ID numbers, which are integers between 0 and 999. Every person has a unique ID; in particular, the ID of Captain Picard is 0. Some values between 0 and 999 may be unused, which means that the total number of crew members may be less than 1,000. The personnel database is a text file, which contains between 1 and 1,000 lines. Each line includes two distinct IDs, separated by a single space. The second ID specifies the supervisor of the person with the first ID. For example, the line “200 100” indicates that Person 100 is the supervisor of Person 200. Example file: 1 0 2 0 200 100 300 100 200 100 300 200
Problem 5A (50 points)
Implement a program that counts the number of crew members, that is,the number of distinct IDs in the personnel file. For instance, the file shown above includes six distinct IDs: 0, 1, 2, 100, 200, and 300.
Input and output:
Your program should input the name ofthe personnel file, and print the number of distinct IDs in the file. The file name is a string, the length of which is between 1 and 60.
Example:
OCommand Prompt C:homework5a Enter name of the input file: personnel.txt The crew includes 6 membersExplanation / Answer
Here is the required code in Java. Each and every statements are explained well using comments. Go through and let me know if you have any doubts. Thanks.
// Crew.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Crew {
public static void main(String[] args) {
/**
* Defining a scanner to read user input, and getting filename
*/
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name of the input file: ");
String fileName = scanner.nextLine();
/**
* Defining a file from filename
*/
File file = new File(fileName);
/**
* Defining an arrayList to store crew ids
*/
ArrayList<Integer> crewList = new ArrayList<Integer>();
try {
/**
* Re initializing the scanner to read from input file
*/
scanner = new Scanner(file);
/**
* Looping through each line in the file
*/
while (scanner.hasNext()) {
// getting line
String line = scanner.nextLine();
// splitting line by whitespace
String ids[] = line.split(" ");
/**
* Extracting 2 ids in the line
*/
int id1 = Integer.parseInt(ids[0]);
int id2 = Integer.parseInt(ids[1]);
/**
* Adding each id to the array list if the list doesn't contain
* the id already
*/
if (!crewList.contains(id1)) {
crewList.add(id1);
}
if (!crewList.contains(id2)) {
crewList.add(id2);
}
}
/**
* After reading, size of array list will be the number of distinct
* crew members
*/
System.out.println("The crew includes " + crewList.size()
+ " members");
} catch (FileNotFoundException e) {
/**
* input file not found
*/
e.printStackTrace();
} catch (Exception e) {
/**
* error happened while trying to split the line by whitespace or
* trying to convert string to integer, possibly happens due to
* invalid input file
*/
System.out.println("Invalid file format");
}
}
}
//crew.txt (which I have used)
1 0
10 255
100 200
0 100
200 155
147 159
155 256
200 210
278 200
741 200
/*OUTPUT*/
Enter name of the input file: crew.txt
The crew includes 13 members