Click here to Skip to main content
15,892,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

Java
/**
 *
 * @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.

C#
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

Java
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++;
        }
    }
}

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.
Posted
Updated 18-Apr-16 6:13am
v2
Comments
Richard MacCutchan 18-Apr-16 8:00am    
You need to step through that code in the debugger to see what is happening. Your use of the count variable looks suspicious, to say the least.
Member 11420363 18-Apr-16 9:37am    
Count seems to be fine, the problem seems to be that the scanner looks at the text "Answers.txt" and not the actual file. However if I declare this file the program does not seem to be able to find that file.
Richard MacCutchan 18-Apr-16 9:41am    
Then you should look at the documentation to see why.
Richard MacCutchan 18-Apr-16 11:56am    
You need to find out where the files are and use the correct paths when running the program. Alternatively use the FileDialog class to locate them.

1 solution

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.
 
Share this answer
 
Comments
Member 11420363 18-Apr-16 14:34pm    
I have found a solution to this as can be seen at the bottom of the what have I tried section. However it can not find the specified file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900