1

I have the following code snippet:

long count = 0;
Scanner s = null;
s = new Scanner(new BufferedReader(new FileReader("data.txt")));
while(s.hasNext()){
    System.out.println("number of words : "+ ++count);
}

The data.txt file contains some words separated by tabs and spaces. I want to detect when the words are separated by only space and when they are separated by tabs. More specifically, I want to detect the current delimiter being used by Scanner object.

vefthym
  • 7,422
  • 6
  • 32
  • 58
AL AMIN
  • 13
  • 1
  • 4
  • What code have you implemented so far in order to determine the delimiter? – Ceelos Feb 18 '16 at 23:38
  • I did not implement any. I want to know if there is any way. – AL AMIN Feb 18 '16 at 23:41
  • `Scanner` use the delimiter you specify, or it *"by default matches whitespace"* (javadoc). That means that if your file contains `abcf`, it will return `a`, `b`, `c`, and `d`. There is no common separator, so *"I want to detect **the current delimiter** being used by Scanner"* is a impossible request. – Andreas Feb 18 '16 at 23:50

2 Answers2

0

You have to write your code. I do not know why you want to use Scanner for reading a file but it is not the fastest way to do it. Anyway

package javaapplication52;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 *
 * @author Alex
 */
public class JavaApplication52 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        File file = new File("test.txt");
        int flag = -1, counter = 0, start_flag = 0;
        try {

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
                String tmp_string = "";
                counter = 0;
                char[] CharAr = line.toCharArray();

                for (final char c : CharAr) {
                    counter++;
                    if (c == ' ') {
                        System.out.println("whiteSpace");
                        flag = 1;
                    } else if (c == '\t') {
                        System.out.println("tab!");
                        flag = 1;
                    } else if (start_flag == 1) {
                        if (flag == 1) {
                            System.out.println("tmp_sting: " + tmp_string);
                            tmp_string = "";

                        }
                        if (counter == CharAr.length && flag == 0) {
                            tmp_string += c;
                            System.out.println("tmp_sting: " + tmp_string);
                            tmp_string = "";
                        }
                        flag = 0;
                        tmp_string += c;
                        //System.out.println("C: "+c);
                    } else {
                        start_flag = 1;
                        flag = 0;
                        tmp_string += c;
                    }

                }
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

}

Fastest way to iterate over all the chars in a String: Fastest way to iterate over all the chars in a String

Community
  • 1
  • 1
a.s.p.
  • 328
  • 1
  • 3
  • 12
0

I think you want a FilterReader since you are using Files. You can determine whether a character is whitespace by using Character.isWhitespace(char c). Basic implementation would be:

class YourFilterReader extends FilterReader{
    private int count = 0;

    protected YourFilterReader(Reader in) {
        super(in);
    }

    @Override
    public int read() throws IOException{
        int read;
        boolean ws;
        do{
            read = super.read();
            ws = Character.isWhitespace(read);
            if ( ws ) count++;
        } while(ws);

        return read; 
    }
    public int getCount() {
        return count;
    }
}    

and run it with:

YourFilterReader fr = new YourFilterReader(new FileReader("..."));
while ( fr.read() != -1);
System.out.println(fr.getCount());
fr.close();
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66