Storing text file Lines into an array without a know length

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 5912 Views - Last Post: 02 October 2010 - 06:55 PM Rate Topic: -----

#1 sport10   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Storing text file Lines into an array without a know length

Posted 02 October 2010 - 03:39 PM

My current problem is that I am writing a program that will read two text files. One has thousands of words in it, and the other one has as many as I want to put in it. The program has to read the words in the first file, and then compare those words to the second file to see if they match. If they do the program will then store the word in to a third file, and it will say "Valid" next to the word, if the words don't match it will still store the word in the third file, but instead will say "Invalid". I cannot figure out how to store the words into the array, because I can't figure out how to get the length of the array. Here's my code, I know the problem in the code I have is ArrayIndexOutOfBounds because the length is currently set to zero. Any Suggestions? BTW I CANNOT use ArrayLists it must be in an array.
import java.io.*;

public class Driver {
	private FileReader mReader,idReader;
	private BufferedReader mobyReader,myIdentifierReader;
	private FileOutputStream vStream;
	private PrintWriter validatedStream;
	private String mLine, idLine,mLineArray[],idLineArray[];
	private int idIndex = 0,mobyIndex = 0;
	
	public Driver(){
	try{
		mReader = new FileReader("WordListMoby.txt");
		mobyReader = new BufferedReader(mReader);
		idReader = new FileReader("myIdentifiers.txt");
		myIdentifierReader = new BufferedReader(idReader);
		vStream = new FileOutputStream("validatedIdentifiers.txt.");
		validatedStream = new PrintWriter(vStream);
		mLineArray = new String[mobyIndex];
		idLineArray = new String[idIndex];
		mLine = mobyReader.readLine();
		idLine = myIdentifierReader.readLine();
		for(int i=0;mLine!=null;i++){
			mLineArray[i]=mLine;
			mLine = mobyReader.readLine();
			mobyIndex++;
		}
	}catch(IOException e){
		System.out.println("I/O exception");
		}
	}
	public static void main(String[] args) {
		Driver d = new Driver();
	}
}


This post has been edited by sport10: 02 October 2010 - 03:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Storing text file Lines into an array without a know length

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 04:02 PM

So 2 choices...
or you make the array as large as it would be needed for the worst case or you regenerate it

String[] line = new String[100];
....
.... fill the line

    i++;
    if(i == line.length) {   // oups array overflow
      // make one 100 bigger
      String[] tmp = new String[line.length + 100];
      // copy it
      for(j = 0; j < line.length; j++)
          tmp[j] = line[j];
      // and use it
      line = tmp;
    }
    line[i] = file input


or if you are lazy the Arrays class has a copyOf() method that will do the same thing
String[] line = new String[100];
....
.... fill the line

    i++;
    if(i == line.length) {   // oups array overflow
      // make one 100 bigger
      line = Arrays.copyOf(line, line.length + 100);
    }
    line[i] = file input


Was This Post Helpful? 1
  • +
  • -

#3 sport10   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 04:18 PM

Thanks Much PBL! Solved the problem I was having.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 04:23 PM

View Postsport10, on 02 October 2010 - 05:18 PM, said:

Thanks Much PBL! Solved the problem I was having.

:^: and thanks for the Thank
Was This Post Helpful? 0
  • +
  • -

#5 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 04:47 PM

BTW I CANNOT use ArrayLists it must be in an array.


Good that you knew the best solution though. :) Did you ever think of looking up the ArrayList source to see how they approached the same problem?
Was This Post Helpful? 0
  • +
  • -

#6 sport10   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 04:49 PM

No, I never did. I'm going to get into arrayLists here shortly in class, I figure I would just wait until then to start dealing with arrayLists. Is it a similar ordeal when using arrayLists as well?
Was This Post Helpful? 0
  • +
  • -

#7 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 04:54 PM

Absolutely,
The essentials of array lists is an array and a length counter. When the length counter gets too big, it creates a bigger array and copies all the old contents over.
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 04:54 PM

View Postcfoley, on 02 October 2010 - 05:47 PM, said:

BTW I CANNOT use ArrayLists it must be in an array.


Good that you knew the best solution though. :) Did you ever think of looking up the ArrayList source to see how they approached the same problem?

Check it more than once :) as for Vector

When you add an element it calls a method checkCapacity()
if the array is full it just does as my first posted code does:
- create an array the double length of the orifinal one (in my code I just added 100)
- copy the old array into this new one (with a for() loop not with Arrays.copyOf()
- assign the new array to the old one

All the replies on this site recommending to newbies to use ArrayList (because more efficient) are just male cattle excrement. ArrayList in the behind does exactly the same thing. Better to let the newbies learn the concept the hard way.
Was This Post Helpful? 0
  • +
  • -

#9 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 05:00 PM

Haha. I don't think I've ever told a newbie to use an array list (or Vector), at least not without showing the DIY method. :)
Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 05:07 PM

View Postcfoley, on 02 October 2010 - 06:00 PM, said:

Haha. I don't think I've ever told a newbie to use an array list (or Vector), at least not without showing the DIY method. :)

:^:
But there a few here that always propose correct (but too much evolved) solutions to newbies as they have to show they know everything :)
Was This Post Helpful? 1
  • +
  • -

#11 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 05:11 PM

I hope that's not me. :)

Sometimes I do write cryptic code... but that's only because I've found a problem interesting, and fancied writing in a different style from my serious code... and after writing and testing it, I figure I might as well post it.
Was This Post Helpful? 1
  • +
  • -

#12 sport10   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 05:22 PM

Ok, since you guys are still reading my post :smile2: I'm running into another problem. I thought after I got the content of the two files stored into the array, I could just do a simple mLineArray[i]=idLineArray[j], and then write the code over. But, it appears that it is not catching the words as being equal. Here's the code:
import java.io.*;
import java.util.Arrays;

public class Driver {
	private FileReader mReader,idReader;
	private BufferedReader mobyReader,myIdentifierReader;
	private FileOutputStream vStream;
	private PrintWriter validatedStream;
	private String mLine, idLine,mLineArray[],idLineArray[];
	
	public Driver(){
	try{
		int i,j;
		mReader = new FileReader("WordListMoby.txt");
		mobyReader = new BufferedReader(mReader);
		idReader = new FileReader("myIdentifiers.txt");
		myIdentifierReader = new BufferedReader(idReader);
		vStream = new FileOutputStream("validatedIdentifiers.txt.");
		validatedStream = new PrintWriter(vStream);
		mLineArray = new String[100];
		idLineArray = new String[100];
		mLine = mobyReader.readLine();
		idLine = myIdentifierReader.readLine();
		for( i=0;mLine!=null;i++){
			if(i==mLineArray.length){
				mLineArray = Arrays.copyOf(mLineArray,mLineArray.length + 100); 
			}
			mLineArray[i]=mLine;
			mLine = mobyReader.readLine();
		for( j=0;idLine!=null;j++){
			if(j==idLineArray.length){
				idLineArray = Arrays.copyOf(idLineArray,idLineArray.length + 100); 
			}
			idLineArray[j]=idLine;
			idLine = myIdentifierReader.readLine();
			if(mLineArray[i]==idLineArray[j]){
				System.out.println(idLineArray[j]);
			}
		}
	}
	}catch(IOException e){
		System.out.println("I/O exception");
		}
	}
	public static void main(String[] args) {
		Driver d = new Driver();
	}
}


The System.out.println(idLineArray[j]) is there just to check to see if it would print out the words that were in both files.

This post has been edited by sport10: 02 October 2010 - 05:22 PM

Was This Post Helpful? 0
  • +
  • -

#13 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 05:32 PM

mLineArray[i]=idLineArray[j]


It's == not equals... but you got that in your code.

Actually, you shouldn't be using == to see if two Strings have the same sequence of letters. I can write "Hello World" on 2 pieces of paper, meaning I have two different Strings. I.e. the are not ==.

Instead you use the equals() method of the String class. That's equivalent of reading my two pieces of paper and seeing if they are teh same. Your if statemant should look something like:

if(mLineArray[i].equals(idLineArray[j])){

Was This Post Helpful? 0
  • +
  • -

#14 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 05:34 PM

this line
if(mLineArray[i]==idLineArray[j]){  



just compare if mLineArray is the same [b]object as idLineArray[j]
which obviously is not the case.... you want to compare the content of them

if(mLineArray[i].equals(idLineArray[j])){



sorry to have started a personal conversation in your thread but cfoley and I have rarely occasions to met :^:

Ninja'd by cfoley great minds thing equal() not == :)

This post has been edited by pbl: 02 October 2010 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#15 sport10   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Storing text file Lines into an array without a know length

Posted 02 October 2010 - 05:37 PM

Thanks for the response cfoley and pbl, but I had tried that prior to posting on here and it did not print out the words that were found in both files. I know that .equals check for content equality, which is what I want, but it is just not printing out the words. Say I put a word "running" into the myIdentifiers.txt file, I know the word "running" is in the MobyWordList.txt file because it is an English word. So, it should print that word out when I do that println command right?

This post has been edited by sport10: 02 October 2010 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2