我的方法输出错误:java.util.InputMismatchException

我的方法输出错误:java.util.InputMismatchException,java,runtime-error,java.util.scanner,Java,Runtime Error,Java.util.scanner,首先,我要说的是,我对这一切都不熟悉,所以请大家对我宽容一点 我的任务是创建一个名为loadLeague()的方法,尽管我付出了所有的努力,但事实证明,这个方法非常困难!我想知道你是否能帮助我。这项任务本身就是关于足球联赛的。我的方法编译时没有任何问题,但它会在Main(或任何其他标准输出)上输出“InputMismatchException”错误。我真的把我的头发拔下来了(好吧,剩下的是什么),我想知道我做错了什么,因为我已经彻底阅读了我所有关于Java的书!!你能看一下我下面的代码并给我指出

首先,我要说的是,我对这一切都不熟悉,所以请大家对我宽容一点

我的任务是创建一个名为
loadLeague()
的方法,尽管我付出了所有的努力,但事实证明,这个方法非常困难!我想知道你是否能帮助我。这项任务本身就是关于足球联赛的。我的方法编译时没有任何问题,但它会在Main(或任何其他标准输出)上输出“InputMismatchException”错误。我真的把我的头发拔下来了(好吧,剩下的是什么),我想知道我做错了什么,因为我已经彻底阅读了我所有关于Java的书!!你能看一下我下面的代码并给我指出正确的方向吗

谢谢大家

另外,我只想对这个论坛说声谢谢,因为它帮助我找到了第一份真正的it工作。我很少在这里发表评论或提问,但我每天晚上都会阅读其他人的评论和提问,这对我的采访很有帮助。非常感谢你

import java.io.*;
import java.util.*;

/**  
 * Class League - An instance of this class represents the teams in a
 * football (or similar) league. It provides a class method for creating
 * a new instance of League by reading the data for the teams from a CSV
 * file.
 * 
 * @author Lewis Jones 
 * @version 1.0
 */

public class League
{
   /* instance variables */
   private String name;  // The name of the league
   private Team[] teams; // An array to hold the teams in the league

   /**
    * Constructor for objects of class League. It sets the name of the league
    * to the String object provided as the first argument and initialises teams
    * to an array of the size provided as the second argument. This constructor 
    * is private as it is intended for use only by the class method loadLeague().
    */
   private League(String aName, int size)
   {
      super();
      this.name = aName;
      this.teams = new Team[size];
   }

   /* class method */
   /**
    * This method creates a new League object by reading the required details from
    * a CSV file. The file must be organised as follows:
    *     name(String), number of teams (int)
    *     team name(String), won(int), drawn(int), lost(int), for(int), against    (int)
    *        and so on for each team
    * Having created the new League object the method should create all the Team 
    * objects (using the data in the file to set their attributes) and add them 
    * to the teams array.
    */
   public static League loadLeague()
   {
      League theLeague = null;
      String pathname = OUFileChooser.getFilename();
      File aFile = new File(pathname);
      Scanner bufferedScanner = null;

      try
      {
         String leagueName;
         int numberOfTeams;

         String teamName;
         int won;
         int drawn;
         int lost;
         int goalsFor;
         int goalsAgainst;
         Scanner lineScanner;
         String currentLine;
         bufferedScanner = new Scanner(new BufferedReader(new FileReader (aFile)));    

         while (bufferedScanner.hasNextLine())
         {
            currentLine = bufferedScanner.nextLine();
            lineScanner = new Scanner(currentLine);
            lineScanner.useDelimiter(",");

            leagueName = bufferedScanner.next();
            numberOfTeams = bufferedScanner.nextInt();

            teamName = bufferedScanner.next();
            won = lineScanner.nextInt();
            drawn = lineScanner.nextInt();
            lost = lineScanner.nextInt();
            goalsFor = lineScanner.nextInt();
            goalsAgainst = lineScanner.nextInt();

            Team aTeam = new Team(lineScanner.next());
            aTeam.setWon(lineScanner.nextInt());
            aTeam.setDrawn(lineScanner.nextInt());
            aTeam.setLost(lineScanner.nextInt());
            aTeam.setGoalsFor(lineScanner.nextInt());
            aTeam.setGoalsAgainst(lineScanner.nextInt());
            Team[] teams = new Team[numberOfTeams];
            teams[numberOfTeams] = aTeam;
            numberOfTeams++;
            theLeague = new League(leagueName, numberOfTeams);
         }
      }  
      catch (Exception anException)
      {
         System.out.println("Error: " + anException);
      }
      finally
      {
         try
         {
            bufferedScanner.close();
         }
         catch (Exception anException)
         {
            System.out.println("Error: " + anException);
         }
      }
      return theLeague;
   }

   /* instance methods */

   /**
    * Displays the league table in tabular format to the standard output
    */
   public void display()
   {
      System.out.println(this.name);
      System.out.format("%20s %2s %2s %2s %2s %2s %2s %    2s\n","","P","W","L","D","F","A","Pt");
      for (Team eachTeam : this.teams)
      {
         System.out.format("%20s %2d %2d %2d %2d %2d %2d %2d\n",
                       eachTeam.getName(), eachTeam.getPlayed(), 
                       eachTeam.getWon(), eachTeam.getDrawn(), 
                       eachTeam.getLost(),eachTeam.getGoalsFor(), 
                       eachTeam.getGoalsAgainst(), eachTeam.getPoints());        
      }
   }

   /**
    * Arrange the elements of teams in their natural order. This will only
    * work if a natural order has been defined for the class Team.
    */
   public void sort()
   {
      // to be written later...
   }
}
编辑:下面是此任务附带的示例(输入)文件。我道歉。昨晚我完全忘了把这个写在我的帖子里

Scottish League Division 1,10
Airdrie United ,3,2,11,14,25
Clyde          ,5,7,4,21,17
Dundee         ,7,2,7,21,18
Gretna         ,10,3,3,43,20
Hamilton Acas  ,7,5,4,19,20
Livingstone    ,6,6,4,21,15
Partick Thistle,8,4,4,25,29
Queen of South ,3,3,10,11,31
Ross County    ,4,4,8,14,24
St Johnstone   ,6,6,4,26,16

我确切地知道问题出在哪里。您的输入文件格式不正确。例如,当您需要“int”时,您正在读取不同的格式,如字符串。由于我没有您的输入,下面是一个快速示例,向您展示如何生成该异常:


示例文件包含以下行:

Sample.txt
1,8,6,3
1,2,无效,3

正如您将看到的,由于第二行生成错误,因此仅打印Sample.txt的第一行。 输出:
1,8,6,3

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at staxpro.StAXPro.main(StAXPro.java:35)
publicstaticvoidmain(字符串[]args){
试一试{
Scanner bufferedScanner=new Scanner(new BufferedReader(new FileReader(“Sample.txt”));
while(bufferedScanner.hasNextLine()){
字符串currentLine=bufferedScanner.nextLine();
扫描仪行扫描仪=新扫描仪(当前行);
lineScanner.useDelimiter(“,”);
int first=lineScanner.nextInt();
int second=lineScanner.nextInt();
//这里是我读取字符串而不是int值的地方
//在输入文件的第二行
int third=lineScanner.nextInt();
int forth=lineScanner.nextInt();
System.out.println(第一+“,“+第二+”,“+第三+”,“+第四”);
}
}捕获(FileNotFoundException ex){
System.err.println(例如getLocalizedMessage());
}
}
编辑:

快速查看您的代码和输入文件,我发现了另一个问题:

teamName = bufferedScanner.next();
            won = lineScanner.nextInt();
            drawn = lineScanner.nextInt();
            lost = lineScanner.nextInt();
            goalsFor = lineScanner.nextInt();
            goalsAgainst = lineScanner.nextInt();

            Team aTeam = new Team(lineScanner.next());
            aTeam.setWon(lineScanner.nextInt());
            aTeam.setDrawn(lineScanner.nextInt());
            aTeam.setLost(lineScanner.nextInt());
            aTeam.setGoalsFor(lineScanner.nextInt());
            aTeam.setGoalsAgainst(lineScanner.nextInt());
你能认出它吗?你读到了#赢#抽#抽#。。。等,然后将它们存储在赢得的绘制的丢失的目标的目标的,但当您要创建团队对象时,您将从扫描仪中读取下一个值。所以基本上,你总是每隔一行读一次!!删除所有内容,但保留此部分:

        Team aTeam = new Team(lineScanner.next());
        aTeam.setWon(lineScanner.nextInt());
        aTeam.setDrawn(lineScanner.nextInt());
        aTeam.setLost(lineScanner.nextInt());
        aTeam.setGoalsFor(lineScanner.nextInt());
        aTeam.setGoalsAgainst(lineScanner.nextInt());
因此,基于上述内容,在输入文件中,每一行必须具有: Stringintintintintint
但如果您注意到,输入文件的第一行是:

Scottish League Division 1,10
这是一个字符串intint。把这些修好,你就可以走了。 这只是我的观察,并没有实际运行您的代码。所以我可能错过了什么。如果您在修复这些后仍有问题,请告诉我

编辑(2):
我修改了你的代码并做了更正。我针对这个输入运行了它:

Scottish League Division 1,10
Airdrie United ,3,2,11,14,25
Clyde          ,5,7,4,21,17
Dundee         ,7,2,7,21,18
Gretna         ,10,3,3,43,20
Hamilton Acas  ,7,5,4,19,20
Livingstone    ,6,6,4,21,15
Partick Thistle,8,4,4,25,29
Queen of South ,3,3,10,11,31
Ross County    ,4,4,4,14,24
St Johnstone   ,6,6,4,26,16

而且效果很好。如果还有其他问题,请告诉我。

import java.io.*;
导入java.util.*;
/**  
*类联盟-该类的一个实例表示联盟中的团队
*足球(或类似)联赛。它提供了一个类方法来创建
*通过从CSV读取球队数据创建联盟的新实例
*文件。
* 
*@作者刘易斯·琼斯
*@version 1.0
*/
公共阶级联盟
{
/*实例变量*/
私有字符串名称;//联盟的名称
私人团队[]团队;//联盟中容纳团队的阵列
/**
*类League对象的构造函数。它设置League的名称
*添加到作为第一个参数提供的字符串对象,并初始化团队
*指向作为第二个参数提供的大小的数组。此构造函数
*是私有的,因为它仅用于类方法loadLeague()。
*/
私人联赛(String aName,队[]队)
{
超级();
this.name=aName;
这个。团队=团队;
}
/*类方法*/
/**
*此方法通过从中读取所需的详细信息来创建新的League对象
*CSV文件。该文件必须按以下方式组织:
*名称(字符串)、团队数量(整数)
*球队名称(字符串)、获胜(整数)、平局(整数)、失利(整数)、赞成(整数)、反对(整数)
*每支球队都是如此
*创建了新的联盟对象后,该方法应创建所有团队
*对象(使用文件中的数据设置其属性)并添加它们
*到团队阵列。
*/
公共静态联盟loadLeague()
{
联盟长度=空;
String pathname=OUFileChooser.getFilename();
File aFile=新文件(路径名);
Scanner bufferedScanner=null;
尝试
{
字符串联盟名称;
国际团队;
字符串组名;
尹元,;
内画;
int丢失;
int目标为:;
内特进球反对;
扫描线扫描仪;
串电流线;
浅黄色
import java.io.*;
import java.util.*;

/**  
 * Class League - An instance of this class represents the teams in a
 * football (or similar) league. It provides a class method for creating
 * a new instance of League by reading the data for the teams from a CSV
 * file.
 * 
 * @author Lewis Jones 
 * @version 1.0
 */

public class League
{
   /* instance variables */
   private String name;  // The name of the league
   private Team[] teams; // An array to hold the teams in the league

   /**
    * Constructor for objects of class League. It sets the name of the league
    * to the String object provided as the first argument and initialises teams
    * to an array of the size provided as the second argument. This constructor 
    * is private as it is intended for use only by the class method loadLeague().
    */
   private League(String aName, Team[] teams)
   {
      super();
      this.name = aName;
      this.teams = teams;
   }

   /* class method */
   /**
    * This method creates a new League object by reading the required details from
    * a CSV file. The file must be organised as follows:
    *     name(String), number of teams (int)
    *     team name(String), won(int), drawn(int), lost(int), for(int), against    (int)
    *        and so on for each team
    * Having created the new League object the method should create all the Team 
    * objects (using the data in the file to set their attributes) and add them 
    * to the teams array.
    */
   public static League loadLeague()
   {
      League theLeague = null;
      String pathname = OUFileChooser.getFilename();
      File aFile = new File(pathname);
      Scanner bufferedScanner = null;

      try
      {
         String leagueName;
         int numberOfTeams;

         String teamName;
         int won;
         int drawn;
         int lost;
         int goalsFor;
         int goalsAgainst;
         Scanner lineScanner;
         String currentLine;
         bufferedScanner = new Scanner(new BufferedReader(new FileReader (aFile)));    
         boolean firstLine = true;
         List<Team> teamsList = new ArrayList<Team>();
         Team[] teams = null;
         while (bufferedScanner.hasNextLine())
         {
            currentLine = bufferedScanner.nextLine();
            lineScanner = new Scanner(currentLine);
            lineScanner.useDelimiter(",");

            if (firstLine) {
                // you originally used "bufferedScanner" which actually 
                // gets the values on the next line, not the current line
                leagueName = lineScanner.next();
                numberOfTeams = lineScanner.nextInt();
                firstLine = false;
                continue;
            }

            Team aTeam = new Team(lineScanner.next());
            aTeam.setWon(lineScanner.nextInt());
            aTeam.setDrawn(lineScanner.nextInt());
            aTeam.setLost(lineScanner.nextInt());
            aTeam.setGoalsFor(lineScanner.nextInt());
            aTeam.setGoalsAgainst(lineScanner.nextInt());
            teamsList.add(aTeam);

         }
         teams = teamsList.toArray(new Team[]{});
         theLeague = new League(leagueName, teams);
      }  
      catch (Exception anException)
      {
         System.out.println("Error: " + anException);
      }
      finally
      {
         try
         {
            bufferedScanner.close();
         }
         catch (Exception anException)
         {
            System.out.println("Error: " + anException);
         }
      }
      return theLeague;
   }

   /* instance methods */

   /**
    * Displays the league table in tabular format to the standard output
    */
   public void display()
   {
      System.out.println(this.name);
      System.out.format("%20s %2s %2s %2s %2s %2s %2s %    2s\n","","P","W","L","D","F","A","Pt");
      for (Team eachTeam : this.teams)
      {
         System.out.format("%20s %2d %2d %2d %2d %2d %2d %2d\n",
                       eachTeam.getName(), eachTeam.getPlayed(), 
                       eachTeam.getWon(), eachTeam.getDrawn(), 
                       eachTeam.getLost(),eachTeam.getGoalsFor(), 
                       eachTeam.getGoalsAgainst(), eachTeam.getPoints());        
      }
   }

   /**
    * Arrange the elements of teams in their natural order. This will only
    * work if a natural order has been defined for the class Team.
    */
   public void sort()
   {
      // to be written later...
   }
}