I seem to be able to load the files in my program. Then I can loop through the lines one time, but if I try a second loop to load the strings into an array that one won't work.

Here's my code. I thank you for any help in advance.

EDIT: Its the 4th while loop that isn't working. The first three are fine.

import java.net.URL;
import java.io.*;

public class SpellChecker{

		BufferedReader words = null;
		BufferedReader newList = null;
		BufferedReader customWords = null;
		BufferedReader customNewList = null;
		File cust = new File("customWords.txt");
		String line  = " ";
		String[] spellList;
		String[] custList;
		String displayLine;
		int i = 0;
		int j = 0;
		
		public SpellChecker(){
			try{
	
				URL url = new URL("edited for privacy");
				words = new BufferedReader( new InputStreamReader(url.openStream()));
				newList = new BufferedReader( new InputStreamReader(url.openStream()));
				FileInputStream file = new FileInputStream(cust);
				customWords = new BufferedReader( new InputStreamReader(file));
				customNewList = new BufferedReader( new InputStreamReader(file));
				
				while((line = words.readLine()) != null){
					i++;
				}
				
				spellList = new String [i+1];
				i = 0;
				
				while((line = newList.readLine()) != null ){
					spellList[i] = line;
					//System.out.println(line);
					i++;
				}
				
				while((line = customWords.readLine()) != null){
					j++;
				}
				
				System.out.println("Loading custom list array");
				custList = new String [j+1];
				j = 0;

				while((line = customNewList.readLine()) != null){
					System.out.println("Array loaded");
					custList[j] = line;
					System.out.println(line);
					j++;
				}
				
			}
			catch(Exception e){
				System.err.print(e);
			}
			
			finally{
	
				try{
					words.close();
				}
				
				catch(Exception e){
					System.err.print(e);
				}
			}
		}//end constructor

Recommended Answers

All 2 Replies

I guess this is because your last two Readers end up using the same underlying stream to read from the source; here file . Because of this, when the third BufferedReader finishes, the end of stream has already being reached for the file and further reading obviously returns null .

Either create a new instance of the FileInputStream for each Reader or reset the stream marker as soon as you are done reading once.

A few observations though:
• You need not make Readers/Writers instance variables since they logically don't form the `state' of your SpellChecker; plus its always better to declare variables at the point of use and persisting BuferedReader serves no purpose here. The same is the case with `i' and `j'; they form a part of your implementation and need not be made instance variables.
• Creating a File instance the way you have done is also *bad*.
• Your code does not close all the open resources.
• Even though this looks like a prototype, try to organize your thoughts and implementation rather than dumping them together.
• When pasting code, make sure you convert tabs to spaces since the rendering of tags is not consistent across implementations. [your editor might render a tab as 2 spaces while this message board might use 8 spaces].

I know this is late, but thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.