Как я могу рассчитать массив объектов метода readAllExams (Scanner s) ?? Выход должен быть чем-л так:Как я могу отобразить массив объектов?
John Morgan 27 'M' 100
Марка Шермана 55 'F' 98
................ ......
Но вместо этого я получаю только номера индексов.
class Exam {
private String firstName;
private String lastName;
private int ID;
private char examType;
private int score;
public Exam(String firstName, String lastName, int ID, char examType, int score)
{
this.firstName = firstName;
this.lastName = lastName;
this.ID = ID;
this.examType = examType;
this.score = score;
}
public String getFirstName()
{
return this.firstName;
}
public String getLastName()
{
return this.lastName;
}
public int getID()
{
return this.ID;
}
public char getExamType()
{
return this.examType;
}
public int getScore()
{
return this.score;
}
public String toString()
{
return this.firstName + " " + this.lastName + " " + this.ID + " " + this.examType + " " + this.score;
}
public boolean equals(Exam object)
{
if(this.equals(object))
return true;
return false;
}
}
import java.io.*;
import java.util.*;
class P2 {
public static void main(String [] args) throws FileNotFoundException
{
Scanner data = new Scanner(new File("Exam.txt"));
Exam[] readObjects = readAllExams(data);
//Exam[] collateObjects = collateExams(readObjects);
for(int i = 0; i < readObjects.length; i++)
{
System.out.println(i);
}
}
public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
{
String firstName = "";
String lastName = "";
int ID = 0;
String examType = "";
char examTypeCasted;
int score = 0;
int index = 0;
Exam[] object = new Exam[50];
while(s.hasNext())
{
//Returns firtsName and lastName
firstName = s.next();
lastName = s.next();
//Returns ID number
if(s.hasNextInt())
{
ID = s.nextInt();
}
else
s.next();
//Returns examType which is 'M' or 'F'
examType = s.next();
examTypeCasted = examType.charAt(0);
if(s.hasNextInt())
{
score = s.nextInt();
}
object[index] = new Exam(firstName, lastName, ID, examTypeCasted, score);
//System.out.println();
index++;
}
readExam(s);
return object;
}
public static Exam readExam(Scanner s)
{
String firstName = "";
String lastName = "";
int ID = 0;
String examType = "";
char examTypeCasted = 0;
int score = 0;
while (s.hasNext())
{
//Returns firtsName and lastName
firstName = s.next();
lastName = s.next();
//Returns ID number
if(s.hasNextInt())
{
ID = s.nextInt();
}
//Returns examType which is 'M' or 'F'
examType = s.next();
examTypeCasted = examType.charAt(0);
if(s.hasNextInt())
{
score = s.nextInt();
}
}
Exam temp = new Exam(firstName, lastName, ID, examTypeCasted, score);
return temp;
}
Nevermind, люди! Я забыл прочитать объекты [i] в цикле for! Спасибо за внимание! – Nikola