Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having trouble loading information from saved file (list.txt) into the arra

ID: 3815277 • Letter: I

Question

I'm having trouble loading information from saved file (list.txt) into the arraylist.

//Driver.java

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;

public class Driver {
   static ArrayList<Student> studentList = new ArrayList<Student>();
   public static void main(String[] args) throws Exception {
       new Driver();
      
   }
  
   public Driver() throws Exception {
       Scanner input = new Scanner(System.in);
      
       System.out.println("Select an option below");
   System.out.println("");
  
   while(true) {
       System.out.println("1: Add Student");
       System.out.println("2: Display Student");
       System.out.println("3: Remove Student");
       System.out.println("4: Save Student List to a File");
       System.out.println("5: Load Student List from a File");
       System.out.println("0: Exit");
  
   int userChoice = input.nextInt();   
   switch(userChoice) {
   case 1:
   addStudent();
   break;
   case 2:
       displayStudent();
   break;
   case 3:
       removeStudent();
       break;
   case 4:
       saveStudent();
       break;
   case 5:
       loadStudent();
       break;
   case 0:
   System.out.println("Thank you! See you again!");
   System.exit(0);
   }
   }
   }
  
   private void loadStudent() throws Exception {
       Scanner input = new Scanner(System.in);
       String loadFile;
       Student newStudent;
       System.out.println("Enter file name to load:");
       loadFile = input.nextLine();
      
       try {
           java.io.File file = new java.io.File(loadFile);
           Scanner reader = new Scanner(file);
           while(reader.hasNext()) {
               String[] list = reader.nextLine().split(",");
               newStudent = new Student();
               (newStudent).setFirstName(list[0]);
               (newStudent).setLastName(list[1]);
               (newStudent).setGender(list[2]);
              
           }
       reader.close();
       System.out.println("File has been loaded.");
       } catch(FileNotFoundException ex) {
           System.out.println("File not Found");
       }
   }
  
  
  
  
   private void saveStudent() throws Exception {
       try (
               java.io.PrintWriter output = new java.io.PrintWriter("StudentList.txt"); ) {
           if(studentList.isEmpty()) {
               System.out.println("Your List is Empty!");
               System.out.println("");
           } else {
               for(Student newStudent : studentList) {
               output.println(newStudent.getFirstName() + "," + newStudent.getLastName()
               + "," + newStudent.getGender());
           }
       }
      
   }
      
       System.out.println("Student List Saved!");
       System.out.println("");
   }
  
  
  
  
   private void addStudent() {
       Student newStudents = new Student();
       Scanner input = new Scanner(System.in);
       String FirstName, LastName, Gender, Major, NetID;
       double GPA;
       int Age, UIN;
       Student students;
      
       if(studentList.size() >= 3) {
           System.out.println("Your list is full!");
       } else {

       System.out.print("Enter First Name: ");
       FirstName = input.nextLine();
       System.out.print("Enter Last Name: ");
       LastName = input.nextLine();
       System.out.print("Enter Student Gender (M for Male and F for Female): ");
       Gender = input.nextLine();
      
       while(!Gender.equalsIgnoreCase("M") && !Gender.equalsIgnoreCase("F")) {
           System.out.print("Please select: M or F, Try Again: ");
           Gender = input.nextLine();
       }


      
       newStudents.setFirstName(FirstName);
       newStudents.setLastName(LastName);
       newStudents.setGender(Gender);

      
       studentList.add(studentList.size(), newStudents);
      
       System.out.println("Added!");
       System.out.println("");

   }
   }
  
   private void removeStudent() {
       Scanner input = new Scanner(System.in);
       String RemoveNetID;
       boolean found = false;
      
       if(studentList.isEmpty()){
           System.out.println("List is empty!");
           System.out.println("");
       } else {
           System.out.print("Enter NetID of Student to remove: ");
           RemoveNetID = input.nextLine();
           for(int i = 0; i < studentList.size(); i++) {
               if(studentList.get(i).getNetID().equalsIgnoreCase(RemoveNetID)) {
                   studentList.remove(i);
                   System.out.println("Removed!");
                   System.out.println("");
                   found = true;
                   break;                  
               }
           }
           if(found) {
              
           } else {
               System.out.println("No Match!");
               System.out.println("");
           }
       }

      
   }
              

   private void displayStudent() {
       if(studentList.isEmpty()) {
           System.out.println("List is empty!");
           System.out.println("");
       } else {
       System.out.println(" Student List : ");
       for(int i = 0; i < studentList.size(); i++) {
           System.out.println(studentList.get(i).toString());
           System.out.println("");
       }
       }

   }
   }

//Student.java


public class Student {
  
   private String FirstName;
   private String LastName;
   private String Gender;

  
   public Student(String FirstName, String LastName, String Gender) {
       this.FirstName = FirstName;
       this.LastName = LastName;
       this.Gender = Gender;

   }
  
   public Student() {
      
   }
  
   public String getFirstName() {
       return FirstName;
   }
  
   public void setFirstName(String FirstName) {
       this.FirstName = FirstName;
   }
  
   public String getLastName() {
       return LastName;
   }
  
   public void setLastName(String LastName) {
       this.LastName = LastName;
   }
  
   public String getGender() {
       return Gender;
   }
  
   public void setGender(String Gender) {
       this.Gender = Gender;
      
   }

  
  
   public String toString() {
       return " First Name: " + FirstName + " Last Name: " + LastName +
               " Gender: " + Gender;
   }

}

//list.txt (this is the file i'm trying to load into the arraylist)

kelly,johnson,F
mike,green,M

Explanation / Answer

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.*;
import java.lang.*;
import java.util.ArrayList;
class Driver {
static ArrayList<Student> studentList = new ArrayList<>();
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
  
System.out.println("Select an option below");
System.out.println("");
  
while(true) {
System.out.println("1: Add Student");
System.out.println("2: Display Student");
System.out.println("3: Remove Student");
System.out.println("4: Save Student List to a File");
System.out.println("5: Load Student List from a File");
System.out.println("0: Exit");
  
int userChoice = input.nextInt();   
switch(userChoice) {
case 1:
addStudent();
break;
case 2:
displayStudent();
break;
case 3:
removeStudent();
break;
case 4:
saveStudent();
break;
case 5:
loadStudent();
break;
case 0:
System.out.println("Thank you! See you again!");
System.exit(0);
}
}
}
  
private static void loadStudent() throws Exception {
Scanner input = new Scanner(System.in);
String loadFile;
Student newStudent;
System.out.println("Enter file name to load:");
loadFile = input.nextLine();
  
try {
java.io.File file = new java.io.File(loadFile);
Scanner reader = new Scanner(file);
while(reader.hasNext()) {
String[] list = reader.nextLine().split(",");
newStudent = new Student();
(newStudent).setFirstName(list[0]);
(newStudent).setLastName(list[1]);
(newStudent).setGender(list[2]);
  
}
reader.close();
System.out.println("File has been loaded.");
} catch(FileNotFoundException ex) {
System.out.println("File not Found");
}
}
  
  
  
  
private static void saveStudent() throws Exception {
try (
java.io.PrintWriter output = new java.io.PrintWriter("StudentList.txt"); ) {
if(studentList.isEmpty()) {
System.out.println("Your List is Empty!");
System.out.println("");
} else {
for(Student newStudent : studentList) {
output.println(newStudent.getFirstName() + "," + newStudent.getLastName()
+ "," + newStudent.getGender());
}
}
  
}
  
System.out.println("Student List Saved!");
System.out.println("");
}
  
  
  
  
private static void addStudent() {
Student newStudents = new Student();
Scanner input = new Scanner(System.in);
String FirstName, LastName, Gender, Major, NetID;
double GPA;
int Age, UIN;
Student students;
  
if(studentList.size() >= 3) {
System.out.println("Your list is full!");
} else {
System.out.print("Enter First Name: ");
FirstName = input.nextLine();
System.out.print("Enter Last Name: ");
LastName = input.nextLine();
System.out.print("Enter Student Gender (M for Male and F for Female): ");
Gender = input.nextLine();
  
while(!Gender.equalsIgnoreCase("M") && !Gender.equalsIgnoreCase("F")) {
System.out.print("Please select: M or F, Try Again: ");
Gender = input.nextLine();
}

  
newStudents.setFirstName(FirstName);
newStudents.setLastName(LastName);
newStudents.setGender(Gender);
  
studentList.add(studentList.size(), newStudents);
  
System.out.println("Added!");
System.out.println("");
}
}
  
private static void removeStudent() {
Scanner input = new Scanner(System.in);
String RemoveNetID;
boolean found = false;
  
if(studentList.isEmpty()){
System.out.println("List is empty!");
System.out.println("");
} else {
System.out.print("Enter NetID of Student to remove: ");
RemoveNetID = input.nextLine();
for(int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i).toString());
boolean x=studentList.get(i).equalsIgnoreCase(RemoveNetID);
if(!x)
{
  
} else {
studentList.remove(i);
System.out.println("Removed!");
System.out.println("");
found = true;
break;
}
}
if(found) {
  
} else {
System.out.println("No Match!");
System.out.println("");
}
}
  
}
  
private static void displayStudent() {
if(studentList.isEmpty()) {
System.out.println("List is empty!");
System.out.println("");
} else {
System.out.println(" Student List : ");
for(int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i).toString());
System.out.println("");
}
}
}

private static Object getNetID() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
//Student.java

public class Student {
  
private String FirstName;
private String LastName;
private String Gender;
  
public Student(String FirstName, String LastName, String Gender) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Gender = Gender;
}
  
public Student() {
  
}
  
public String getFirstName() {
return FirstName;
}
  
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
  
public String getLastName() {
return LastName;
}
  
public void setLastName(String LastName) {
this.LastName = LastName;
}
  
public String getGender() {
return Gender;
}
  
public void setGender(String Gender) {
this.Gender = Gender;
  
}
  
  
public String toString() {
return " First Name: " + FirstName + " Last Name: " + LastName +
" Gender: " + Gender;
}

Object getNetID() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

/*boolean equalsIgnoreCase(String RemoveNetID) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}*/

boolean equalsIgnoreCase(String RemoveNetID) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote