SlideShare a Scribd company logo
1 of 4
Download to read offline
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;
}
}

More Related Content

Similar to I have a Student.java class constructor like thisAnd I need to re.pdf

Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
irshadkumar3
 
-Java programming- Write a class named Junior-java Inherits the Stud.docx
-Java programming-  Write a class named Junior-java  Inherits the Stud.docx-Java programming-  Write a class named Junior-java  Inherits the Stud.docx
-Java programming- Write a class named Junior-java Inherits the Stud.docx
dorisc7
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
명철 강
 

Similar to I have a Student.java class constructor like thisAnd I need to re.pdf (20)

03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
constructer.pptx
constructer.pptxconstructer.pptx
constructer.pptx
 
CMSC 350 HOMEWORK 1
CMSC 350 HOMEWORK 1CMSC 350 HOMEWORK 1
CMSC 350 HOMEWORK 1
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
-Java programming- Write a class named Junior-java Inherits the Stud.docx
-Java programming-  Write a class named Junior-java  Inherits the Stud.docx-Java programming-  Write a class named Junior-java  Inherits the Stud.docx
-Java programming- Write a class named Junior-java Inherits the Stud.docx
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
Unit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptxUnit No 3 Inheritance annd Polymorphism.pptx
Unit No 3 Inheritance annd Polymorphism.pptx
 
1_JavIntro
1_JavIntro1_JavIntro
1_JavIntro
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
BCA Class and Object (3).pptx
BCA Class and Object (3).pptxBCA Class and Object (3).pptx
BCA Class and Object (3).pptx
 
Static keyword.pptx
Static keyword.pptxStatic keyword.pptx
Static keyword.pptx
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 
Principles of Object Oriented Programming
Principles of Object Oriented ProgrammingPrinciples of Object Oriented Programming
Principles of Object Oriented Programming
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
Oop
OopOop
Oop
 

More from arorastores

Need this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdfNeed this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdf
arorastores
 
Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdf
arorastores
 
looking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdflooking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdf
arorastores
 
How do incomplete dominance and codominance differ There is no diff.pdf
How do incomplete dominance and codominance differ  There is no diff.pdfHow do incomplete dominance and codominance differ  There is no diff.pdf
How do incomplete dominance and codominance differ There is no diff.pdf
arorastores
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
Describe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdfDescribe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdf
arorastores
 
dass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdfdass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdf
arorastores
 
Describe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdfDescribe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdf
arorastores
 
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdfAn infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
arorastores
 
A study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdfA study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdf
arorastores
 

More from arorastores (20)

Need this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdfNeed this in JAVA We have N numbers as an array, you need to find a .pdf
Need this in JAVA We have N numbers as an array, you need to find a .pdf
 
Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdf
 
looking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdflooking for help with this question, have another question that.pdf
looking for help with this question, have another question that.pdf
 
In a population of Canadian Eskimos the autosomal recessive gene for.pdf
In a population of Canadian Eskimos the autosomal recessive gene for.pdfIn a population of Canadian Eskimos the autosomal recessive gene for.pdf
In a population of Canadian Eskimos the autosomal recessive gene for.pdf
 
How is that terrorists are able to use media to their advantage an.pdf
How is that terrorists are able to use media to their advantage an.pdfHow is that terrorists are able to use media to their advantage an.pdf
How is that terrorists are able to use media to their advantage an.pdf
 
Identify the three ventral body cavities and the two dorsal body cavi.pdf
Identify the three ventral body cavities and the two dorsal body cavi.pdfIdentify the three ventral body cavities and the two dorsal body cavi.pdf
Identify the three ventral body cavities and the two dorsal body cavi.pdf
 
How do incomplete dominance and codominance differ There is no diff.pdf
How do incomplete dominance and codominance differ  There is no diff.pdfHow do incomplete dominance and codominance differ  There is no diff.pdf
How do incomplete dominance and codominance differ There is no diff.pdf
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdfFruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
Fruits develop from microsporangia receptacles fertilized eggs ovarie.pdf
 
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdfFind all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
Find all equilibrium solutions of the equation dydx = y^2 - 1. Use o.pdf
 
Determine which protist causes each of the following diseases in huma.pdf
Determine which protist causes each of the following diseases in huma.pdfDetermine which protist causes each of the following diseases in huma.pdf
Determine which protist causes each of the following diseases in huma.pdf
 
Describe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdfDescribe the transmission of the meningococcus, and discuss the path.pdf
Describe the transmission of the meningococcus, and discuss the path.pdf
 
dass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdfdass Defining Species art C Gene flow and the biological species conc.pdf
dass Defining Species art C Gene flow and the biological species conc.pdf
 
Describe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdfDescribe one way in which you could use butterfly rearing in the fut.pdf
Describe one way in which you could use butterfly rearing in the fut.pdf
 
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdfCritical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
Critical Thinking Course...PhilosophyIn Chapter 8, we learned the .pdf
 
Can I get a detailed answer on the following question. Bacterial ind.pdf
Can I get a detailed answer on the following question. Bacterial ind.pdfCan I get a detailed answer on the following question. Bacterial ind.pdf
Can I get a detailed answer on the following question. Bacterial ind.pdf
 
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdf
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdfAnother algebraic way to express a constraint is E_1 = E_2, where bot.pdf
Another algebraic way to express a constraint is E_1 = E_2, where bot.pdf
 
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdfAn infinitely long sheet of charge of width L lies in the xy -plane .pdf
An infinitely long sheet of charge of width L lies in the xy -plane .pdf
 
A study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdfA study tracked incidence rates of a blood vessel disorder over time.pdf
A study tracked incidence rates of a blood vessel disorder over time.pdf
 
A distant galaxy was found to have a redshift of 1.8. How far away i.pdf
A distant galaxy was found to have a redshift of 1.8. How far away i.pdfA distant galaxy was found to have a redshift of 1.8. How far away i.pdf
A distant galaxy was found to have a redshift of 1.8. How far away i.pdf
 

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 

I have a Student.java class constructor like thisAnd I need to re.pdf

  • 1. 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); }
  • 2. 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;
  • 3. } 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() {
  • 4. 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; } }