2013-02-01 96 views
0

我正在努力阅读下面的堆栈跟踪异常。我想知道如果你能帮助我吗?如何读取java.util.InputMismatchException堆栈跟踪

我不明白代码中的任何数字(例如数字74,2119和2160,它们代表什么在这个异常?相当字面上,我需要知道我的代码的哪一行导致问题如此我可以解决这个问题。请帮助。

这下面是我的堆栈跟踪,并在它下面是Java代码,我试图解决一起把随任务的样本输入文件。

java.util.InputMismatchException 
    in java.util.Scanner.throwFor(Scanner.java:909) 
    in java.util.Scanner.next(Scanner.java:1530) 
    in java.util.Scanner.nextInt(Scanner.java:2160) 
    in java.util.Scanner.nextInt(Scanner.java:2119) 
    in League.loadLeague(League.java:74) 
    in (Workspace:1) 

在这里,下面是我的Java代码与方法loadLeague,这使我很头痛!

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 

我已经真正完成这个任务奋斗了现在的近一个星期!我希望那里有人来帮助我,因为它现在真的让我感觉到了。请帮忙。任何提示,以查明我错写了哪些代码将非常感激。

谢谢各位,

Lew。

+0

您是通过调试器中的代码来查看哪一行输入发生错误以及它与其他行有什么不同? –

+0

嗨Miserable变量,恐怕我的IDE没有Eclipse之类的调试器。我使用BlueJ。 –

+0

你会学会使用某种远程调试方法。如果你不能用BlueJ来做,那么你可以在命令行上使用jdb。 –

回答

2

这些数字(74,2119和2160)是代码行。堆栈跟踪的最上面一行是发生实际错误的地方,其余是堆栈跟踪中的调用位置。

因此,在这种情况下,Scanner.java:909意味着错误发生在扫描仪类的行909,该扫描程序类在扫描仪中的行1530处调用的函数中,等等。

+0

谢谢你,杰西,这有点帮助。那么,我该如何进入Scanner类,或者说,打开它并纠正我的代码? –

+0

嗯,我建议看看扫描仪API:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html看看你是如何使用它是不正确的。 –