I am currenty in a CPS 181 and this my current program. It is set up in 3 differ
ID: 3873694 • Letter: I
Question
I am currenty in a CPS 181 and this my current program. It is set up in 3 different classes within the same program as well as 2 files.
MyMain Class:
package program2;
import java.io.IOException;
public class MyMain {
public static void main(String[] args) throws IOException {
List list1 = new List();
list1.run();
System.out.println("BYE BYE");
}
}
List Class:
package program2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class List {
ArrayList mylist = new ArrayList();
Person per;
public void run() throws IOException {
int choice = -999;
choice = menu();
while (choice != 0) {
switch (choice) {
case 0: {
System.out.println("You chose 0 leave");
break;
}
case 1: {
System.out.println("You chose: 1) Read from a file ");
fillFromFile();
String str = toString();
System.out.println("The array is " + str);
break;
}
case 2: {
System.out.println("You chose: 2) Combine two files into one list ");
break;
}
case 3: {
System.out.println("You chose: 3) Sort the list by ID number ");
break;
}
case 4: {
System.out.println("You chose: 4) Check for names in the list ");
break;
}
case 5: {
System.out.println("You chose: 5) Check for duplicates and remove them ");
break;
}
case 6: {
System.out.println("You chose: 6) Add names to the list ");
break;
}
case 7: {
System.out.println("You chose: 7) Remove names from the list ");
break;
}
default: {
System.out.println("You chose default");
choice = 0;
break;
}
}
choice = menu();
}
}
public int menu() {
Scanner kb = new Scanner(System.in);
int choice = 0;
System.out.println(" What do you wish to do?");
System.out.println("Put in the number corresponding to your choice ");
System.out.println("1) Read from a file (Enter the name of the file)");
System.out.println("2) Combine two files into one list");
System.out.println("3) Sort the list by an Identification Number");
System.out.println("4) Check for names in the list");
System.out.println("5) Check for duplicates in the list and remove them");
System.out.println("6) Add individual names to the list");
System.out.println("7) Remove names from the list");
System.out.println("0) End the program");
choice = kb.nextInt();
// if (choice != 0)
// choice = 0;
return (choice);
}
public void fillFromFile() throws FileNotFoundException {
String fileName;
Person per;
Scanner kb = new Scanner(System.in);
System.out.print("Input the name of the file you wish to use: ");
fileName = kb.next();
File infile = new File(fileName);
Scanner sc = new Scanner(infile);
String nam;
int ID;
while (sc.hasNext()) {
per = new Person();
nam = sc.next();
ID = sc.nextInt();
System.out.println(nam + " " + ID);
per.name = nam;
per.ID = ID;
System.out.println(per.name + " " + per.ID);
System.out.println(per);
mylist.add(per);
System.out.println(mylist);
}
}
public String toString() {
String arra = "";
System.out.println(mylist.size());
for (int i = 0; i < mylist.size(); i++) {
arra = arra + mylist.get(i).toString();
}
return (arra);
}
}
Person Class:
package program2;
public class Person implements Comparable <Person>{
String name;
int ID;
public Person()
{
}
public int compareTo(Person that) {
int thatID = that.ID;
if (this.ID < thatID)
return -1;
else if (this.ID > thatID)
return 1;
else
return 0;
}
public String toString()
{
String result = this.name + " " + this.ID + " ";
return (result);
}
}
file1.txt
Pat
Vanderbrook
1234
Sue
Johnson
2345
Fred
Clarke
111
Mike
Wilson
666
Ted
O'Neil
3456
Beth
Smith-Jones
1111
Julie
Stinson
6789
5usan
Workman
2345
fred
Clarke
111
file2.txt
Ed
Smith
5656
Ronette
Jones
6767
Ted
O'Neil
3456
Beth
Smith-Jones
1111
Desiree
Franklin
9876
Jasmine
O'grady
5645
Lem
Smith
9843
Ben
Dover
9191
I keep getting an error when I run just the first option so i don't know what to do to move forward
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class List {
ArrayList mylist = new ArrayList();
Person per;
public void run() throws IOException {
int choice = -999;
choice = menu();
while (choice != 0) {
switch (choice) {
case 0: {
System.out.println("You chose 0 leave");
break;
}
case 1: {
System.out.println("You chose: 1) Read from a file ");
fillFromFile();
String str = toString();
System.out.println("The array is " + str);
break;
}
case 2: {
System.out.println("You chose: 2) Combine two files into one list ");
break;
}
case 3: {
System.out.println("You chose: 3) Sort the list by ID number ");
break;
}
case 4: {
System.out.println("You chose: 4) Check for names in the list ");
break;
}
case 5: {
System.out.println("You chose: 5) Check for duplicates and remove them ");
break;
}
case 6: {
System.out.println("You chose: 6) Add names to the list ");
break;
}
case 7: {
System.out.println("You chose: 7) Remove names from the list ");
break;
}
default: {
System.out.println("You chose default");
choice = 0;
break;
}
}
choice = menu();
}
}
public int menu() {
Scanner kb = new Scanner(System.in);
int choice = 0;
System.out.println(" What do you wish to do?");
System.out.println("Put in the number corresponding to your choice ");
System.out.println("1) Read from a file (Enter the name of the file)");
System.out.println("2) Combine two files into one list");
System.out.println("3) Sort the list by an Identification Number");
System.out.println("4) Check for names in the list");
System.out.println("5) Check for duplicates in the list and remove them");
System.out.println("6) Add individual names to the list");
System.out.println("7) Remove names from the list");
System.out.println("0) End the program");
choice = kb.nextInt();
// if (choice != 0)
// choice = 0;
return (choice);
}
public void fillFromFile() throws FileNotFoundException {
String fileName;
Person per;
Scanner kb = new Scanner(System.in);
System.out.print("Input the name of the file you wish to use: ");
fileName = kb.next();
String nam;
int ID;
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
per = new Person();
nam = sCurrentLine;
nam=nam +" "+ br.readLine();
ID=Integer.parseInt(br.readLine());
per.name = nam;
per.ID = ID;
mylist.add(per);
System.out.println(mylist);
}
} catch (IOException e) {
e.printStackTrace();
}
// while (sc.hasNext()) {
//
//
// ID = sc.nextInt();
//
// System.out.println(per.name + " " + per.ID);
// System.out.println(per);
// mylist.add(per);
// System.out.println(mylist);
// }
}
public String toString() {
String arra = "";
System.out.println(mylist.size());
for (int i = 0; i < mylist.size(); i++) {
arra = arra + mylist.get(i).toString();
}
return (arra);
}
}
public class Person implements Comparable<Person> {
String name;
int ID;
public Person() {
}
public int compareTo(Person that) {
int thatID = that.ID;
if (this.ID < thatID)
return -1;
else if (this.ID > thatID)
return 1;
else
return 0;
}
public String toString() {
String result = this.name + " " + this.ID + " ";
return (result);
}
}
import java.io.IOException;
public class MyMain {
public static void main(String[] args) throws IOException {
List list1 = new List();
list1.run();
System.out.println("BYE BYE");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.