2014-07-02 53 views
0

大家好! 夥計們我有一個簡單的代碼的問題。我找不到錯在哪裏。每次我想運行的程序中,我得到這個消息:關於java.util.InputMismatchException的一些東西?

Exception in thread "main" java.util.InputMismatchException 
    at java.util.Scanner.throwFor(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at java.util.Scanner.nextDouble(Unknown Source) 
    at TeamMMA.<init>(TeamMMA.java:17) 
    at ShowTeamFrame.main(ShowTeamFrame.java:8) 

這是一個簡單的PROGRAMM,我希望有一個開始:

import java.text.DecimalFormat; 
public class MMACompetitors { 
private String name; 
private double average; 
public MMACompetitors(String name, double average){ 
    this.name=name; 
    this.average=average; 

} 
public String getName(){ 
    return name; 
} 
public double getAverage(){ 
    return average; 
} 
public String getAverageString(){ 
    DecimalFormat decFormat=new DecimalFormat(); 
    decFormat.setMaximumIntegerDigits(0); 
    decFormat.setMaximumFractionDigits(3); 
    decFormat.setMinimumFractionDigits(3); 
    return decFormat.format(average); 
    } 
} 

第二類:

import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import java.awt.GridLayout; 

@SuppressWarnings("serial") 

public class TeamMMA extends JFrame{ 
public TeamMMA() throws IOException{ 
    MMACompetitors mma; 
    @SuppressWarnings("resource") 
    Scanner keyboard = new Scanner(new File("MMAStatisticBullyTeam.txt")); 
    for(int num=1;num <=5;num++){ 
     mma=new MMACompetitors(keyboard.nextLine(),keyboard.nextDouble()); 
     keyboard.nextLine(); 
     addCompetitorInfo(mma); 
     } 
    setTitle("Bully's"); 
    setLayout(new GridLayout(9,2,20,30)); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    pack(); 
    setVisible(true); 
    } 
void addCompetitorInfo(MMACompetitors mma){ 
    add (new JLabel (" "+ mma.getName())); 
    add (new JLabel(mma.getAverageString())); 
} 
} 

和主類:

import java.io.IOException; 


class ShowTeamFrame { 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
new TeamMMA(); 
    } 
} 

謝謝你的支持!我感謝每一個評論,我會接受所有建議!

+0

你在MMAStatisticBullyTeam.txt中有什麼? –

+0

就這一小塊文字:斯特凡 0.4 的Borko 0.6 總理 0.8 Arnauda 0.13 – user3747038

回答

0

Inside for循環從掃描程序讀取的兩行預期文件中的數據以匹配類型存在,以嘗試按該順序讀取。

mma=new MMACompetitors(keyboard.nextLine(),keyboard.nextDouble()); 
keyboard.nextLine(); 

文件中的文本應該是任何文本,後跟一個double值,後跟一個空格,然後再次跟隨任何帶有行尾的文本。例如,

This line is a for nextLine method 
12.01 The value is twelve do zero one 

該空間是掃描儀的默認分隔符。空格是第二行對於nextDouble來說很重要,以識別要讀取的文本片段爲double。當使用nextLine進行讀取時,此默認分隔符不適用,因爲它直到行尾。

第二行不是以有效數字開頭,或者數字後沒有空格。

由於它在for循環中,所以應該重複使用此模式的一組行。

0

如果線17 keyboard.nextLine()這個解釋可以幫助你:

此異常由Scanner拋出,表明該 檢索期望類型不匹配的格局令牌 ,或令牌超出預期類型的​​範圍。