943,846 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 265
  • Java RSS
Jan 17th, 2009
0

Help with my code.

Expand Post »
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.
Last edited by l_03; Jan 17th, 2009 at 9:36 pm.
Similar Threads
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Jan 18th, 2009
0

Re: Help with my code.

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with managing .dat files
Next Thread in Java Forum Timeline: Long cannot be dereferenced?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC