0
夥計們我從讀取文件中的數據時遇到問題。從文件問題讀取數據(Java初學者語言)
當我試圖從文件輸入後計算GPA時,我只能得到名稱和ID的權利。檢查全碼here
Name: Ali Ahmad
ID: 2009000
Exception in thread "main" java.lang.NullPointerException
at IcsProject.display(IcsProject.java:136)
at IcsProject.main(IcsProject.java:45)
`
我真的需要這方面的幫助。我花了幾個小時來修復它,但我不能。
public static void readFromFile() throws IOException
{
System.out.println("Enter the file name ");
String FileNameInput = keyboard.next();
Scanner filein = new Scanner(new FileInputStream(FileNameInput));
stuName = filein.nextLine();
ID = filein.nextInt();
filein.next();
semNum = filein.nextInt();
courseCode=new String[semNum][];
creditHours=new int[semNum][];
grade=new String[semNum][];
semCode=new int[semNum];
for(int i = 0; i < semNum; i++)
{
semCode[i] = filein.nextInt();
filein.next();
semCourses = filein.nextInt();
for(int j = 0; j < semCourses; j++)
{
courseCode[i][j]=filein.next();
creditHours[i][j]=filein.nextInt();
grade[i][j]=filein.next();
}
}
}
輸入數據是這樣的:
Ali Ahmad
2009000
Semesters 2
093
Courses 2
IAS100 2 A+
PE100 2 B
101
Courses 4
ICS103 3 A+
MATH101 4 B
PHYS101 4 C+
CHEM101 4 D+
這是該方法計算GPA等能正常工作,手動輸入。
public static void display(PrintWriter output)
{
output.println("Name: "+stuName);
output.println("ID: "+ID);
output.println("");
GPA=new double[semNum];
for(int i=0;i<semNum;i++)
{
GPA[i]=0.0;
double creditHoursSum=0.0;
for(int j=0;j<courseCode[i].length;j++)
{
GPA[i]+=creditHours[i][j]*gradeValue(grade[i][j]);
creditHoursSum+=creditHours[i][j];
}
GPA[i]=GPA[i]/creditHoursSum;
}
cumulativeGPA=0.0;
for(int i=0;i<semNum;i++)
cumulativeGPA+=GPA[i];
cumulativeGPA=cumulativeGPA/semNum;
for(int i=0 ; i < semNum ; i++)
output.printf("GPA for semester %d = %.2f\t%s\n",semCode[i],GPA[i],gpaLvl(GPA[i]));//Using `Printf` To limit the number of digits of the GPA. I learned the method from our Course Book.Took me a while to get it though.
output.printf("\nCumulative GPA = %.2f\n",cumulativeGPA);
output.println("");
}
謝謝。我得到這個'名稱:阿里·艾哈邁德· ID:2009000 累積GPA = NaN'應該顯示像這樣'名稱:阿里·艾哈邁德· ID:2009000 GPA爲093學期= 3.50其次榮譽 GPA的學期101 = 2.67 Good Standing 累積GPA = 2.84' – 2011-05-29 09:27:28
@MohammadFadin您應該向我們展示'display'方法的代碼和計算'累積GPA'的代碼。 – Howard 2011-05-29 09:29:00
@霍華德我剛剛發佈了它。我還在問題開始時發佈了完整代碼的鏈接。 – 2011-05-29 09:32:45