我正在编写一个程序,需要一年的时间作为用户输入并返回当年的超级碗赢家(使用超级碗获奖者列表的文本文件)。它编译正确,但当我尝试执行程序时,我收到错误:
线程中的异常" main" java.util.NoSuchElementException:找不到行 在java.util.Scanner.nextLine(Scanner.java:1540) 在Hw5pr4.main(Hw5pr4.java:17)
我做错了什么?
文字档案:
1967 Green Bay Packers
1968 Green Bay Packers
1969 New York Jets
1970 Kansas City Chiefs
1971 Baltimore Colts
1972 Dallas Cowboys
1973 Miami Dolphins
1974 Miami Dolphins
1975 Pittsburgh Steelers
1976 Pittsburgh Steelers
1977 Oakland Raiders
1978 Dallas Cowboys
1979 Pittsburgh Steelers
1980 Pittsburgh Steelers
1981 Oakland Raiders
1982 San Francisco 49ers
1983 Washington Redskins
1984 Los Angeles Raiders
1985 San Francisco 49ers
1986 Chicago Bears
1987 New York Giants
1988 Washington Redskins
1989 San Francisco 49ers
1990 San Francisco 49ers
1991 New York Giants
1992 Washington Redskins
1993 Dallas Cowboys
1994 Dallas Cowboys
1995 San Francisco 49ers
1996 Dallas Cowboys
1997 Green Bay Packers
1998 Denver Broncos
1999 Denver Broncos
2000 St. Louis Rams
2001 Baltimore Ravens
2002 New England Patriots
2003 Tampa Bay Buccaneers
2004 New England Patriots
2005 New England Patriots
2006 Pittsburgh Steelers
2007 Indianapolis Colts
2008 New York Giants
2009 Pittsburgh Steelers
2010 New Orleans Saints
2011 Green Bay Packers
2012 New York Giants
2013 Baltimore Ravens
2014 Seattle Seahawks
2015 New England Patriots
源代码:
import java.util.*;
import java.io.*;
public class Hw5pr4
{
public static void main(String[] args) throws IOException{
File winners = new File("SuperBowlWinners.txt");
Scanner reader = new Scanner(winners);
String[][] data = new String[49][2];
int index = 0;
while(reader.hasNext()){
int yr = reader.nextInt();
String year = Integer.toString(yr);
data[index][0] = year;
reader.nextLine();
data[index][1] = reader.nextLine();
index++;
}
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a year, or to exit, enter STOP");
String input = keyboard.nextLine();
String winner = "Not found";
boolean found = false;
while(input!="STOP"){
for(int i=0; i<49; i++){
if(input.equals(data[i][0])){
winner = data[i][1];
found = true;
}
}
System.out.println("Enter a year, or to exit, enter STOP");
input = keyboard.nextLine();
}
if(found)
System.out.println(winner);
else
System.out.println("Error: the year is not in the data.");
}
}
答案 0 :(得分:1)
首先,您正在阅读下一行很多次:
while(reader.hasNext()){
int yr = reader.nextInt();
String year = Integer.toString(yr);
data[index][0] = year;
reader.nextLine();
data[index][1] = reader.nextLine();
index++;
}
尝试删除第一个reader.nextLine(),因为您读取了数据中的下一行[index] [1] = reader.nextLine();
你还有另一个错误 - 而(输入!=“停止”)。对于字符串比较,您必须使用while(!input.equals(“STOP”)。
我还注意到你的代码中还有其他一些错误。例如,我认为你必须把这段代码
if(found)
System.out.println(winner);
else
System.out.println("Error: the year is not in the data.");
在while循环中打印团队输入年份。您还必须在while循环的每次迭代开始时将found设置为false。你会发现其他错误。
编辑:这是一个代码示例,您应该如何在JAVA中编写代码(这不是完美的,但您可以看到应该考虑的内容):
File winners = new File("D:\\stack.txt");
Scanner reader = new Scanner(winners);
HashMap<Integer, String> map = new HashMap<>();
while (reader.hasNext()) {
map.put(reader.nextInt(), reader.nextLine().trim());
}
Scanner keyboard = new Scanner(System.in);
String input = "";
do {
System.out.println("Enter a year, or to exit, enter STOP");
try {
input = keyboard.nextLine().toUpperCase();
if(map.containsKey(Integer.parseInt(input))){
System.out.println(map.get(Integer.parseInt(input)));
} else {
System.out.println("Error: the year is not in the data.");
}
} catch (Exception e) {
//This is just an example of handling exception, you must handle it better
//e.printStackTrace();
if(!input.equals("STOP"))
System.out.println("You must enter a valid number...");
}
} while (!input.equals("STOP"));
答案 1 :(得分:0)
您不止一次触发nextLine。您应该将nextLine()存储在while()结构开头的变量中并使用它。
例如,如果您在第10行,您将尝试阅读第10行和第11行。