繁体   English   中英

使用扫描仪读取文本文件

[英]Using Scanner to read from text file

我正在编写一个程序,该程序需要一年作为用户输入,并返回当年的超级碗冠军(使用超级碗冠军名单的文本文件)。 它可以正确编译,但是当我尝试执行程序时出现错误:

线程“主”中的异常java.util.NoSuchElementException:在Hw5pr4.main(Hw5pr4.java:17)的java.util.Scanner.nextLine(Scanner.java:1540)处找不到行

我究竟做错了什么?

文本文件:

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

首先,您要阅读很多时间的下一行:

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(),因为您读取了data [index] [1] = reader.nextLine();中的下一行;

您还存在另一个错误-while(input!=“ STOP”)。 对于字符串比较,必须使用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"));

您不止一次触发nextLine。 您应该在while()结构的开始处将nextLine()存储在变量中,并使用它。

例如,如果您在第10行,您将尝试阅读第10行和第11行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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