I have a Studentjava class constructor like this And I need

I have a Student.java class constructor like this:

And I need to read a text file that has information like this:

How can I read data from this file to an array (no arraylist) using loops? My code doesn\'t work. Here\'s what I have so far:

Thanks in advance!

Solution

Correct Code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;

public class Student {
   private String lastName;
   private String FirstName;
   private String idNo;
   private Course courseArray[];
   private String grade;
   private double gpa;
   static int filecouses=0;
   public static Course courses[] = new Course[100];
public Student(String lastName,String FirstName,String idNo,Course[] courses,String grade,double gpa)
{
   this.setLastName(lastName);
   this.setFirstName(FirstName);
   this.setIdNo(idNo);
   this.setcourseArray(courses);
   this.setGrade(grade);
   this.setGpa(gpa);
  
}
public Student(){
  
}
   public static void main(String[] args) throws IOException {
      
FileReader file=new FileReader(\"student.txt\");
BufferedReader br=new BufferedReader(file);

String inputLine=br.readLine();

String nines=\"-999\";

while(inputLine!=null)
{
   if(!(inputLine.equals(nines))){
      
   Student s=new Student();
   String[] arr=inputLine.split(\",\");
   s.setLastName(arr[0]);
   System.out.println(arr[0]);
   s.setFirstName(arr[1]);
   s.setIdNo(arr[2]);
   inputLine=br.readLine();}

  
   while((inputLine!=null)&&!(inputLine.equals(nines))){
      
       String[] ar=inputLine.split(\",\");
       System.out.println(ar[0]);
       String course=(ar[0]);
       String credits=(ar[1]);
       String grade=(ar[2]);
      
       Course c=new Course(course, credits, grade);
       /*System.out.println(filecouses);
       System.out.println(c);
       System.out.println(inputLine);*/
       inputLine=br.readLine();
       courses[filecouses++]=c;
      
   }
   if(inputLine.equals(nines)){
      
       inputLine=br.readLine();
      
       String[] a=inputLine.split(\",\");
       Student st=new Student();
       st.setGpa(Double.parseDouble(a[1]));
       st.setGrade(a[0]);
       inputLine=br.readLine();
   }
  
  
  
}


   }
   String getLastName() {
       return lastName;
   }
   void setLastName(String lastName) {
       this.lastName = lastName;
   }
   String getFirstName() {
       return FirstName;
   }
   void setFirstName(String firstName) {
       FirstName = firstName;
   }
   String getIdNo() {
       return idNo;
   }
   void setIdNo(String idNo) {
       this.idNo = idNo;
   }
   Course[] getCourses() {
       return courseArray;
   }
   void setcourseArray(Course[] courseArray) {
       this.courseArray = courseArray;
   }
   String getGrade() {
       return grade;
   }
   void setGrade(String grade) {
       this.grade = grade;
   }
   double getGpa() {
       return gpa;
   }
   void setGpa(double gpa) {
       this.gpa = gpa;
   }

}
class Course {
String course;
String credits;
String grade;
public Course(String course,String credits,String grade){
   this.course=course;
   this.credits=credits;
   this.grade=grade;
}

}

I have a Student.java class constructor like this: And I need to read a text file that has information like this: How can I read data from this file to an array
I have a Student.java class constructor like this: And I need to read a text file that has information like this: How can I read data from this file to an array
I have a Student.java class constructor like this: And I need to read a text file that has information like this: How can I read data from this file to an array

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site