0
代碼的邏輯有什麼問題?有兩種方法:readAllExams創建並返回一個對象數組,也調用另一個方法readExam它返回對象。通過掃描儀對象是一個文本文件,其中有10行不同的人的姓名,身份證,中期或最終和得分,例如:約翰麥格雷戈2'F'100.那麼我在這裏做錯了什麼?該方法給出如下的結果:[LExam; @ 717da562。在此先感謝,夥計們!代碼的邏輯有什麼問題?
public static void main(String [] args) throws FileNotFoundException
{
Scanner data = new Scanner(new File("Exam.txt"));
Exam[] tempObject = readAllExams(data);
System.out.println(tempObject);
}
public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
{
readExam(s);
String firstName = "";
String lastName = "";
int ID = 0;
String examType = "";
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();
if(s.hasNextInt())
{
score = s.nextInt();
}
object[index] = new Exam(firstName, lastName, ID, examType, score);
System.out.println();
index++;
}
return object;
}
public static Exam readExam(Scanner s)
{
String firstName = "";
String lastName = "";
int ID = 0;
String examType = "";
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();
}
else
s.next();
//Returns examType which is 'M' or 'F'
examType = s.next();
if(s.hasNextInt())
{
score = s.nextInt();
}
}
Exam temp = new Exam(firstName, lastName, ID, examType, score);
return temp;
}
您正在打印引用數組而不是該數組中元素的引用變量的地址。使用for循環並打印數組中的每個元素,如'tempObject [i];' – Prudhvi 2015-02-11 04:53:28
這是對象溫度的地址。嘗試訪問它的方法/屬性來獲得您想要的輸出。 – Raghav 2015-02-11 04:53:33
1.給考試一個體面的'toString()'實現,以及2。不要試圖盲目打印出一個數組。要麼遍歷打印每個項目的數組,要麼使用'Arrays.toString(myArray)'並打印。這個問題已經被問及**很多次**。 – 2015-02-11 04:53:47