I have this program that needs to read diffrent files for diffrent selected items in a JComboBox. I have gotten the JComboBox and everything to work but now when i try to call the Course_loader code it wont run. Here is the code of where i try to call it from:
public static String CourseName;
public static String PlayerName;
public Start_round(){
try {
Course_loader cl = new Course_loader();
cl.loadCourse(CourseName);
} catch (IOException e) {
}
And now here is the code of the file reader:
import java.io.*;
public class Course_loader {
public static String holes;
public void loadCourse(String s) throws IOException{
File f = new File("courses\\"+s+".txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
holes = reader.readLine();
reader.close();
System.out.println("it ran");
}
}
When ever i call the code it dosnt print out the “it ran”. I have checked over my code my self but i am new to the File reader stuff. Please help. Thanks in advance.
oh and the file is there and it has these words in it
test
and when ever it try to print the string it is null which proves that the stuff isnt running.
You’re silently ignoring
IOException.Change the exception handling in
Start_roundto this:This should generate some output that will give you an idea of what’s going wrong with the
FileReadercode, for example, File not found issues.Another tip: Always put your
reader.close()call in afinallyblock. In your case this means doing some specific exception handling in yourloadCoursemethod. Search around for java filereader, and similar searches to look for best practices. Also, do some work learning about Exceptions, try/catch/finally if you don’t understand that stuff very well.