Question: why doesnt this code read and edit text files: / * * Click nbfs: / / nbhost / SystemFileSystem / Templates / Licenses / license

why doesnt this code read and edit text files: /*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package cpan211.lab3_starter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args){
final int size =100;
Professor[] professors = new Professor[size];
Student[] students = new Student[size];
int pInt =0;
int sInt =0;
try {
FileReader fr = new FileReader(new File("People.txt"));
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine())!= null){
String[] parts = line.split("+");
String title = parts[0];
String name = parts[1];
String major = parts[2];
System.out.println(title);
System.out.println(name);
System.out.println(major);
if (title.equals("Professor")){
String department = parts[2];
professors[pInt++]= new Professor(title, name, department);
} else if (title.equals("Student")){
String degree = parts[2];
students[sInt++]= new Student(title, name, degree);
}
}
br.close();
fr.close();
} catch (IOException e){
System.out.println("An error occurred: "+ e.getMessage());
}
System.out.println("Professors:");
for (int i =0; i < pInt; i++){
System.out.println(professors[i]);
}
System.out.println("
Students:");
for (int i =0; i < sInt; i++){
System.out.println(students[i]);
}
writeToFile(professors, pInt, "Professor.txt");
writeToFile(students, sInt, "Student.txt");
}
private static void writeToFile(Person[] people, int count, String fileName){
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){
for (int i =0; i < count; i++){
writer.write(people[i].toString());
writer.newLine();
}
} catch (IOException e){
System.out.println("An error occurred: "+ e.getMessage());
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!