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.
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();
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)
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);
private Scanner answersSC = new Scanner(answersFile);
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++;
}
}
}
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.