In this project, you will input student data from a file create student objects
ID: 3920018 • Letter: I
Question
In this project, you will
input student data from a file
create student objects
add student objects to an array
write the student object data to standard output.
Download the input text file studentInput3.txt. This file contains four sets of data. Each set of data includes four lines that correspond to one student’s data. The first line is the student’s first and last name, the second line is the student’s id number, the third is the student’s year and the fourth contains the student’s major.
Modify your RunStudent program from project 2 to create a Student array, read the sets of data, create a student object, add the object to the array, and print the array. There are many different ways to do this, but we will do it in the most basic way.
The first question is: do we know how many sets of data are in the file? If we do, and if this number is not likely to change, a for loop and an array are fine to use. An array has a set size that cannot be changed. So, if we know we will only have 10 students, we can create a Student array of size 10 and use a for loop to traverse the input file.
If we have a file, but do not know how many students are contained in the file, an array might not be our best bet. An ArrayList has room for 10 elements by default, but can grow and shrink. An ArrayList has methods to add elements that do the work for us if the list gets too large for the current size. But deep down inside, an ArrayList contains an array.
For this project, we know that we will only have four students, so we will create an array of size 4. Since we have only four students, this means we will have four sets of data. This makes reading from the file easy: a for loop that executes four times and reads the four lines of data during each loop iteration.
Reading the data from the file can be tricky. It would be best to get this part of the program working first before you proceed to the rest of the program.
It is always important to make sure the data structures appropriately contain the data. The last step will be to create a method in the RunStudent class that writes the content of the array to standard output.
Your program will do the following:
Open the input file for reading (hard code the name of the input file – studentInput3.txt).
Create an array of size 4 of type Student
Create a Student variable (and other variables as needed)
The for loop should execute four times and do the following:
Read the data for one student from the input file
Use the Student variable to create a Student object
Add the student object to the array (use the loop control variable as the index to the array)
Finally, after the loop has completed execution, call the method printStudentArray() and send to this method the Student array. This method will be created in the RunStudent class. It takes one parameter – a Student array – and prints the contents of the array.
The printStudentArray method will be contained in the RunStudent class. The following is the method header:
public static void printStudentArray(Student[] s)
Notes
You only need ONE Student object. Declare the Student variable before the for loop begins and create a new Student object using a constructor method inside the loop
The student’s year is stored as a char but printed as the entire word.
printStudentArray should call the display method in the Student class – no need to reinvent the wheel here
------------------------------------
studentinput3.txt ->
---
---------
import java.util.*;
import java.io.*;
public class RunStudent
{
public static void main(String[] args)
{
try{
FileReader fr = new FileReader("studentInput.txt");
Scanner sc = new Scanner(fr);
String name = sc.nextLine();
int idNumber = Integer.parseInt(sc.nextLine());
char year = sc.nextLine().charAt(0);
String major = sc.nextLine();
JaisonStudent ob = new JaisonStudent();
ob.setName(name);
ob.setIdNumber(idNumber);
ob.setYear(year);
ob.setMajor(major);
ob.display();
}
catch(Exception e){
e.printStackTrace();
}
}
}
-------
class Student
{
public String name;
public int idNumber;
public char year;
public String major;
// No argument constructor
Student()
{
this.name = "";
this.idNumber = 0;
this.year = ' ';
this.major = "";
}
// constructor
Student(String name, int idNumber, char year, String major)
{
this.name = name;
this.idNumber = idNumber;
this.year = year;
this.major = major;
}
// accessors method
public String getName()
{
return this.name;
}
public int getIdNumber()
{
return this.idNumber;
}
public char getYear()
{
return this.year;
}
public String getMajor()
{
return this.major;
}
// mutator method
public void setName(String name)
{
this.name = name;
}
public void setIdNumber(int idNumber)
{
this.idNumber = idNumber;
}
public void setYear(char year)
{
this.year = year;
}
public void setMajor(String major)
{
this.major = major;
}
// isplay method that nicely displays the values in the student
// object: name, id number, year and major
public void display()
{
System.out.println("Name : " + this.getName());
System.out.println("ID : " + this.getIdNumber());
System.out.println("Year : " + this.getYear());
System.out.println("Major : " + this.getMajor() + " ");
}
}
Explanation / Answer
RunStudent.class
import java.io.File;
import java.util.Scanner;
public class RunStudent {
public static void printStudentArray(Student[] s)
{
for(int i=0;i<=4;i++)
{
s[i].display();
}
}
public static void main(String[] args) {
try {
Student student[]=new Student[5];
int i=0;
Scanner sc=new Scanner(new File("studentinput3.txt")); //getting text file data
while(sc.hasNext()) {
if(i<5)
{
String name=sc.nextLine();
int id=Integer.parseInt(sc.nextLine());
char year=sc.nextLine().charAt(0);
String major=sc.nextLine();
student[i]=new Student(name,id,year,major); //initializing student constructor
i++;
}
}
printStudentArray(student); //printing Student array
sc.close();
}catch(Exception e) {System.out.println();}
}
}
Student.class
public class Student {
private String name;
private int id;
private char year;
private String major;
public Student(String name, int id, char year, String major) {
this.name = name;
this.id = id;
this.year = year;
this.major = major;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public char getYear() {
return year;
}
public String getMajor() {
return major;
}
//display data of student
public void display()
{
System.out.println("Name : " + this.getName());
System.out.println("ID : " + this.getId());
System.out.println("Year : " + this.getYear());
System.out.println("Major : " + this.getMajor());
}
}
Output:
Name : Douglas Reynholm
ID : 12345
Year : F
Major : Bollocking
Name : Maurice Moss
ID : 20193
Year : O
Major : Computer Science
Name : Roy Trenneman
ID : 30193
Year : S
Major : Computer Information Systems
Name : Jen Barber
ID : 49112
Year : J
Major : Management
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.