Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

whats wrong with my code: / * * Click nbfs: / / nbhost / SystemFileSystem / Templates / Licenses / license - default.txt to change

whats wrong with my code: /*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package cpan211.lab3_starter;
import java.io.BufferedReader;
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("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 {
FileWriter w = new FileWriter(fileName);
for (int i =0; i < count; i++){
w.write(people[i].toString()+"
");
}
w.close();
} catch (IOException e){
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions