0

I'm trying to read multiple lines of a txt file in Java and I can't figure out how to get it to read the next line. I've tried a while loop and when I run my program nothing comes out. What I did was a while (line != null) { so it could read the next line but I've had no luck getting it to complete the task.

This is how the text file reads

20141003
20131105
19990205
20080304,20080305,20080306
19990206,hello,20141001,200003
20050505
July
November,August
19640503
19980707
19642199
20141013
import java.util.*;

public class Tokens1 {
  public static TextFileInput myFile;
  public static StringTokenizer myTokens;
  public static String[] list;
  public static String line;


  public static void main(String[] args) {
    myFile = new TextFileInput("project1.txt");
    line = myFile.readLine();
    myTokens = new StringTokenizer(line, ",");

    list = new String[myTokens.countTokens()];

    int i = 0;
    while (myTokens.hasMoreTokens()) {
      list[i] = myTokens.nextToken();
      i++;
    }

    for (int j = 0; j < list.length; j++)
      System.out.println(list[j]);
  }

}
durron597
  • 31,968
  • 17
  • 99
  • 158
user3371154
  • 17
  • 1
  • 6
  • ["`StringTokenizer` is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of `String` or the `java.util.regex` package instead."](http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) – durron597 Oct 17 '14 at 19:05
  • what is `TextFileInput`? – Hagai Oct 17 '14 at 19:09
  • You could see this: http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java – Massimo Oct 17 '14 at 19:12

1 Answers1

-1

You can do your line reading in a while loop as well. The following is a fairly typical implementation that reads each line of a file until eof. I'm not sure why your implementation of a while loop didn't work, so posting your code would help.

while ((line = reader.readLine()) != null)
{
    myTokens = new StringTokenizer(line, ",");

    list = new String[myTokens.countTokens()];

    int i = 0;

    while (myTokens.hasMoreTokens())
    {
        list[i] = myTokens.nextToken();
        i++;
    }
}

And I would second the notion to use split().

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • I'm still only getting it to print out the first line. – user3371154 Oct 17 '14 at 19:24
  • I have a question. When I try to print the array outside the while loop I only get the first line and when I print it inside the while loop I get every line separated the way I want it. Is the array not holding the values in? I need the array to later remove anything that isnt 8 digits and also then sort those valid dates. I'm sorry to bother you about this. – user3371154 Oct 17 '14 at 20:15
  • @user3371154 Please post new questions as separate questions. This practice makes finding pre-existing solutions much easier. Make sure to post the code that you're using to print out the array, because the way you're printing it is almost certainly the issue at hand. – MarsAtomic Oct 17 '14 at 20:53
  • it's the same code but i'll make a new question. i didn't want it to comes off as spam if i made another question about the same program. – user3371154 Oct 17 '14 at 21:36