扫描仪不会使用.nextline()读取文件。 [英] Scanner will not read file with .nextline().

查看:62
本文介绍了扫描仪不会使用.nextline()读取文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个java类,它扫描两个文档并将从它们中获取的数据保存在某些数组中。这使用以下代码。



I am writing a java class which scans through two documents and saves data taken from them in certain arrays. This uses the following code.

/**
 *
 * @author Joseph
 */
import java.util.*;
import java.io.*;

public class DocumentRead {
    public int[] questionNum = new int[10];
    public String[] questionText = new String[10];
    public String[] firstAns = new String[10];
    public String[] secondAns = new String[10];
    public String[] thirdAns = new String[10];
    public int[] correctAns = new int[10];
    
    private Scanner questionsSC = new Scanner("Questions.txt");
    private Scanner answersSC = new Scanner("Answers.txt");
    
    private int count = 0;
    
    public DocumentRead()
    {
        while((questionsSC.hasNextLine()) && (answersSC.hasNextLine()))
        {
            questionNum[count] = count++;
            questionText[count] = questionsSC.nextLine();
            firstAns[count] = answersSC.nextLine();
            secondAns[count] = answersSC.nextLine();//Line 35
            thirdAns[count] = answersSC.nextLine();
            correctAns[count] = Integer.parseInt(answersSC.nextLine());
            count++;
        }
    }
}





然而,当我运行代码时出现以下错误。





However, when I run the code I get the following error.

Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Scanner.java:1540)
	at cwq5.DocumentRead.<init>(DocumentRead.java:35)





看起来扫描器答案SC不会逐行遍历文档Answers.txt。请有人可以解决这个问题。



我尝试过:



扫描仪查看记事本文档Answers.txt,看起来像这样



Olaf



Snowlaf



Yolaf



1



9000年前



900年前



90年前



1



1700s



1800s



1900s



3







nock



码头



2



Rocket



Boost



导弹



1



5%



15%< br $>


2

5%



3



mesosphere



平流层



thermospher e />


2



冰熊



海熊



白熊



2


一杯银色



一杯金



一杯钻石



2



1839



1925


2012



1



我也有将代码更改为下面的代码





It looks like the scanner answersSC will not iterate through the document "Answers.txt" line by line. Please can someone solve this problem.

What I have tried:

The scanner looks through the Notepad document "Answers.txt" which looks like this

Olaf

Snowlaf

Yolaf

1

9000 years ago

900 years ago

90 years ago

1

1700s

1800s

1900s

3

lock

nock

dock

2

Rocket

Boost

Missile

1

5%

15%

2
5%

3

mesosphere

stratosphere

thermosphere

2

ice bear

sea bear

white bear

2

cup of silver

cup of gold

cup of diamonds

2

1839

1925

2012

1

I have also changed the code to the one below

import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

public class DocumentRead {
    public int[] questionNum = new int[10];
    public String[] questionText = new String[10];
    public String[] firstAns = new String[10];
    public String[] secondAns = new String[10];
    public String[] thirdAns = new String[10];
    public int[] correctAns = new int[10];
    
    private File questionsFile = new File("Questions.txt");
    private File answersFile = new File("Answers.txt");
    
    
    private Scanner questionsSC = new Scanner(questionsFile);//FileNotFoundException
    private Scanner answersSC = new Scanner(answersFile);//FileNotFoundException
    
    private int count = 0;
    
    public DocumentRead()
    {
        while((questionsSC.hasNextLine()) && (answersSC.hasNextLine()))
        {
            questionNum[count] = count + 1;
            questionText[count] = questionsSC.nextLine();
            firstAns[count] = answersSC.nextLine();
            JOptionPane.showMessageDialog(null, firstAns[count]);
            secondAns[count] = answersSC.nextLine();
            thirdAns[count] = answersSC.nextLine();
            correctAns[count] = Integer.parseInt(answersSC.nextLine());
            count++;
        }
    }
}



但是解释器在所述行检测到未报告的异常FileNotFoundException。有没有人知道在不使用固定文件目录的情况下查找文件的方法。此外,该程序适用于多台计算机。


However the interpreter has detected a unreported exception FileNotFoundException at the stated lines. Does anybody know a way of finding the file without using a fixed file directory. In addition this program is meant to work on multiple computers.

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



看起来你忘了检查是否有下一行。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Looks like you forgot to check if there is a next line.


这篇关于扫描仪不会使用.nextline()读取文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆