简体   繁体   中英

Something about java.util.InputMismatchException?

everyone! Guys I have a problem with one simple code. I can't find where I made something wrong. Every time I want a run the program I get this message:

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)

And this is the simple programm , which I want a start:

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);
   }
}

The second class:

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()));
}
}

And the Main class:

import java.io.IOException;


 class ShowTeamFrame {

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

Thank you for your support! I appreciate every comment and i will accept all advice!

Inside for loop the two lines that are read from Scanner expect data in the file to exist in matching type as being attempted to read in that order.

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

The text in file should be a line any text followed by a double value followed by a space and then again followed any text with end of line. For example

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

The space is default delimiter for Scanner. The space is 2nd line is important for the nextDouble to identify the piece of text to read as double. This default delimiter does not apply when nextLine is used to read, as it reads upto end of line.

Either 2nd line is not starting with valid number or there is no space after the number.

Since it is in for loop the set of lines with this pattern should repeat.

If line 17 is keyboard.nextLine() this explanation can help you:

This Exception Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM