- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我已经说过我对这一切都是新手,所以请对我宽容点。
我收到了一项任务,要创建一个名为 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”时,您正在读取不同的格式,例如字符串。由于我没有您的输入,这里有一个简单的示例来向您展示如何生成该异常:
示例文件包含以下行:
示例.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)
public static void main(String[] args) {
try {
Scanner bufferedScanner = new Scanner(new BufferedReader(new FileReader ("Sample.txt")));
while (bufferedScanner.hasNextLine()) {
String currentLine = bufferedScanner.nextLine();
Scanner lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
int first = lineScanner.nextInt();
int second = lineScanner.nextInt();
// Here is where I read a string than an int value
// on the 2nd line of the input file
int third = lineScanner.nextInt();
int forth = lineScanner.nextInt();
System.out.println(first + ", " + second + ", " + third + ", " + forth);
}
} catch (FileNotFoundException ex) {
System.err.println(ex.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());
你能发现它吗?您读取 #wins、#drawns 等,并将它们存储在 won、drawn、lost、goalsFor 中strong>、goalsAgainst,但是当您要创建 Team 对象时,您将从扫描仪中读取 NEXT 值。所以基本上,你总是会每隔一行阅读!!删除所有内容,但保留此部分:
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());
因此,根据上述内容,在您的输入文件中,每一行必须具有:字符串、int、int、int、int、int
但如果您注意到,输入文件的第一行是:
Scottish League Division 1,10
这是一个字符串、int、int。解决这些问题,你就可以开始了。这只是我的观察,尚未实际运行您的代码。所以我可能会错过一些东西。如果修复这些问题后仍然存在问题,请告诉我。
编辑(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.*;
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...
}
}
关于java - 我的方法输出错误 : java. util.InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14637825/
这个问题已经有答案了: Scanner double value - InputMismatchException (2 个回答) 已关闭2 年前。 为什么 Java 在使用 Scanner.next
我正在实现一个驱动程序来测试决策树,但遇到输入不匹配异常。我可以做什么来解决这个问题? 这是我的代码: import java.io.FileNotFoundException; /** * Loa
static long arrayMaxMin[]; public static void main(String[] args) { try{ Scanner in = new
这个问题在这里已经有了答案: Scanner is skipping nextLine() after using next() or nextFoo()? (23 个回答) 5年前关闭。 impor
简介 我在捕获 InputMismatchException 时遇到问题。当我搜索 InputMismatchException 时,我已经阅读了该网站上出现在第一页上的所有线程。我在阅读这些帖子时找
while(inputbook.hasNext()){ id = inputbook.nextInt(); name = inputbook.next(); year = in
我有这个代码: import java.util.Scanner; public class Maggiore3Valori { public static void main(String[
public static void main(String[] args) { try { Scanner scan = new Scanner(new Fi
当输入 55.5 作为第一个提示的答案时,我收到以下错误。输入 50 时我没有收到错误消息。 Exception in thread "main" java.util.InputMismatchExc
为什么我遇到了 inputMismatchException?当输入仅为整数时,结果会很好,但是当我使用双输入时,编译器会抛出 inputMismatchException。 public stati
我有一个任务: 5-8)(求最高分)编写一个程序,提示用户输入学生人数以及每个学生的姓名和分数,最后显示分数最高的学生的名字。 当我运行此代码并输入“john”等名称时,它运行良好,但当我尝试输入“J
我尝试以最简单的方式使用扫描仪: 代码: double gas, efficiency, distance, cost; Scanner scanner = new Scanner(System.in
我试图提示用户输入 x 坐标,但是当我输入十进制数(例如 2.1)作为输入时,这会导致问题。如何解决该问题? import java.util.Scanner; public cl
所以我一直在做一项任务,要求我们建立一个“患者经理”,根据紧急级别确定首先治疗的患者。我遇到的主要问题是,当用户在紧急栏中输入数字以外的内容时,它会使程序崩溃并给我一个 InputMismatchEx
public static void main(String[] args) throws FileNotFoundException { File file = new File("data
我写了下面这段Java代码。当运行它并输入任何值(定义的值,例如 latte,或任何其他值,例如 az 整数)时,我得到一个 InputMismatchException。 据我所知,此异常意味着输入
我希望用户输入一行文本,只要 x 不等于 numOfContestans 的值。当我运行代码时,我收到一个 InputMismatchException。有人知道如何解决这个错误吗? try {
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 7 年前。 我有文本文
我无法找到错误的根源。我所做的就是从文件中读取文本 public static void main(String[] args) throws Exception { int T; S
我在将非整数输入整数字段时遇到问题。我只是采取预防措施,这样如果其他人使用/处理我的程序,他们就不会得到此 InputMismatchException。 当我在 input 变量中输入非数字字符时,
我是一名优秀的程序员,十分优秀!