Reputation: 31
I searched a lot of pages and couldn't find a solution to this:
The following code is not my own. I was copying it from a youtube video tutorial to see if I could create a game and execute it. Unfortunately, a try catch exception method says that the java program Game
can't read from my data.txt file which is 1kb in size and its in the same directory as my Game
and Question
classes.
There are two .java files and 1 .txt file comprised in this program. It worked like a charm for the guy doing the tutorial on youtube, maybe because he compiled and ran it from the command line, but not for me (I use drJava.jar as my compiler and to run the program).
When I tried to compile from the command line I got 5 errors saying there were some issues with symbols. It compiles fine with drJava.
Game.java follows:
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Game {
private static ArrayList<Question> questions = new ArrayList<Question>();
private static void init() {
try {
// This is where the problem occurs, can't read data.txt
List<String> lines = Files.readAllLines(Paths.get("data.txt"));
for (int i = 0; i < lines.size(); i += 5) {
Question q = new Question(lines.get(i), lines.get(i + 1), lines.get(i + 2), lines.get(i + 3), lines.get(i + 4));
}
} catch (Exception e) {
System.out.println("Couldn't read file data.txt. System will now exit");
System.exit(-1);
}
}
private static void loop() {
Scanner scan = new Scanner(System.in);
while (questions.size() > 0) {
Question q = questions.remove(0);
System.out.println(q.text);
for (int i = 0; i < q.answers.length; i++) {
System.out.println(i + " " + q.answers[i]);
}
int input = scan.nextInt();
if (input < 0 || input > q.answers.length - 1) {
System.out.println("input is invalid");
System.exit(-1);
}
if (q.rightAnswer.equals(q.answers[input])) {
System.out.println("Right");
} else {
System.out.println("Right");
}
}
}
public static void main(String[] args) {
init();
loop();
}
}
Question.java follows:
public class Question {
public String text;
public String[] answers;
public String rightAnswer;
public Question(String text, String... answers) {
this.text = text;
this.answers = answers;
this.rightAnswer = answers[0];
for (int i = 0; i < answers.length; i++) {
int randomIndex = (int) (Math.random() * answers.length);
String tmp = answers[i];
answers[i] = answers[randomIndex];
answers[randomIndex] = tmp;
}
}
}
data.txt file:
what is 4x4?
14
25
44
24
what does R stand for in RGBA?
red
rocket
right
road
Upvotes: 0
Views: 831
Reputation: 21
I know nothing of drJava, but it looks like you've misspelled the Java.io.File package in your import. It might be that drJava is ignoring this and therefore you don't have proper access to that library's code. See if you can print the actual exception you are getting in the catch with System.out.println(e);
This printed a index out of bounds exception.
So an index out of bounds exception means that you are trying to access something outside the bounds of your list (or array). I suspect that your code is actually successfully reading the file. The error actually occurs when you try to loop through the lines afterwards. Make sure that there are no empty lines in your data file because the loop you are using is quite dangerous. Think about it in English. For as long as i is less than my list length, do stuff. Particularly, "size 11" makes me think you have 1 extra line which allows the loop to run one more time than it is supposed to.
Upvotes: 2
Reputation: 505
Is Data.txt in the same directory as your program is running in? You May be running into a problem of looking in the wrong directory. Thus it's not a case of ot being able to read the file, but finding it.
Alternatively if it is pointing to the correct place, check the permissions on the file and make sure it allowed to be read.
Upvotes: 1