Search code examples
javastringtokenize

Java StringTokenizer.nextToken() skips over empty fields


I am using a tab (/t) as delimiter and I know there are some empty fields in my data e.g.:

one->two->->three

Where -> equals the tab. As you can see an empty field is still correctly surrounded by tabs. Data is collected using a loop :

 while ((strLine = br.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(strLine, "\t");
    String test = st.nextToken();
    ...
    }

Yet Java ignores this "empty string" and skips the field.

Is there a way to circumvent this behaviour and force java to read in empty fields anyway?


Solution

  • Thank you at all. Due to the first comment I was able to find a solution: Yes you are right, thank you for your reference:

     Scanner s = new Scanner(new File("data.txt"));
     while (s.hasNextLine()) {
          String line = s.nextLine();
          String[] items= line.split("\t", -1);
          System.out.println(items[5]);
          //System.out.println(Arrays.toString(cols));
     }