0
package skater;
import java.io.*;
import java.util.*;
public class TestSkater
{
public static void main(String[]args) throws IOException
{
Scanner sc = new Scanner(new File("C:/pairs.txt"));
String name1 = null, name2 = null, Country = null;
double [] Technical = new double [8];
double [] Performance = new double[8];
try {
while(sc.hasNext())
{
name1=sc.nextLine();
name2 = sc.nextLine();
Country = sc.nextLine();
for (int t = 0; t<Technical.length; t++)
{
Technical[t] = sc.nextDouble();
}
for(int y = 0; y<Performance.length; y++)
{
Performance[y] = sc.nextDouble();
}
// Skater [] skaters = new Skater[20];
Skater S1 = new Skater(name1,name2,Country,Technical,Performance);
System.out.println(S1);
sc.nextLine();
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
System.out.print("Bad program");
}
}
}
每當我運行我的代碼而沒有嘗試並捕獲時,我得到一個InputMismatchException
。它是從一個文件中讀取的,該文件包含所有球隊的得分和國家名稱。它的格式如下:運行後類型不匹配
smith
jones
australia
4.2 5.1 3.8 2.9 5.0 4.6 4.9 4.3
4.9 4.8 5.8 3.8 4.9 4.6 5.0 4.5
lennon
murray
england
2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8
我知道nextLine()
告訴它看數據的下一行,而我使用的陣列Performance
和Technical
的nextDouble()
,告訴它看接下來是兩倍。
我不明白爲什麼它給了我這個錯誤。我認爲這是因爲當它移動到下一個團隊的名字時,它仍然將它看作是數組Performance
的雙倍數,但我無法弄清楚如何解決這個問題。
我看到你打印出Skater對象後,它創建。它是否正確輸出第一個對象並在第二個對象上失敗(如您所示,指示問題是在先前的條目性能值之後讀取該名稱),還是在第一個項目上失敗(指示解析一般輸入的問題)? – DFreeman
完成讀取第一對的所有對象後,它會立即失敗。只要讀取下一個名字的第一個對象,它就會崩潰 – Mina09090