954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with my code.

import java.io.BufferedReader;
import java.io.FileReader;
import TokenizerProcess.StringTokenizer;
import javax.swing.JOptionPane;

public class ReadFile {

public static int count = 0;
public static String[][]file = new String[6][3];


public void isReadFile(){
SubjectInput input = new SubjectInput();

try{
BufferedReader reader = new BufferedReader
(new FileReader("SubjectFiles/subjects.txt"));
String line;


while((line = reader.readLine())!=null){
StringTokenizer tokenFile = new StringTokenizer(line);

file[count][0] = tokenFile.nextToken();
file[count][1] = tokenFile.nextToken();
file[count][2] = tokenFile.nextToken();
count++;

}


}catch(Exception e){
JOptionPane.showMessageDialog(null,"File not found.");
}
}
public static void main (String args[]){
System.out.println(Readfile.file[1][1]);

}

}


Hello, can someone help me, i dont know what is the problem with this. If i want to output the file[1][1], the output is "null". But if I will use the "sysout" at the isReadFile method, it will output the file[1][1]. I dont know what is the problem. Help. Thank you ahead.

l_03
Light Poster
37 posts since Oct 2008
Reputation Points: 5
Solved Threads: 0
 

Either make the String array as a member of ReadFile or make the method isReadFile static. Anyways, if your code looks the same way as you have posted, you are missing the invocation of isReadFile in main() which actually initializes the two dimensional String array.

If all you need to do is to read the contents of the file once, there is no need to maintain static member variables or impart any state to the ReadFile class; just make it a utility method and you should be good to go.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You