-2

I am parsing a file data on the base of \t what I want if found more then one \t start parsing from next new line and start arraylist words from 0.

public static void readFile() throws IOException {
  String line;
  ArrayList<String> words = new ArrayList<String>();

  try {
    BufferedReader inFile = new BufferedReader (new FileReader ("/weather.txt"));

    while ((line = inFile.readLine()) != null) {
      StringTokenizer token= new StringTokenizer(line,"\t");
      while (token.hasMoreTokens()) {   
        words.add(token.nextToken());
      }

      /*
       * function to print the values
       */                   
      getMetadataTriple(words);
    }
  }
Qaiser Mehmood
  • 975
  • 6
  • 21
  • 33
  • From the [Javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html): 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. – Keppil Aug 16 '12 at 11:31

1 Answers1

0

Try the following regex solution:

public static void readFile() throws IOException
{
    String line;
    ArrayList<String> words = new ArrayList<String>();    
    try
    {
        BufferedReader inFile = new BufferedReader(new FileReader("weather.txt"));

        while ((line = inFile.readLine()) != null)
        {
            String[] temp = line.split("\\t");
            for (String s : temp)
            {
                if(!s.isEmpty())
                {
                    words.add(s);
                }
                else //another \t
                {
                    words.clear();
                    break;
                }   
            }

            /*
             * function to print the values
             */
            getMetadataTriple(words);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}
Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49