3

Is there a way to obtain the number of token in a string obtained by Method Scanner in Java?

I mean, i can use s = sc.nextLine() to obtain an input line as a string. But on this string I cannot use lenght() method cause it gives me the total number of characters (I think).

Are existing any standard methods to obtain the number of token? Thanks in advance

alessandrob
  • 1,605
  • 4
  • 20
  • 23
  • 1
    I think there is no such way. Scanner gives you next token when you have entered it. It means, it doesn't _know_ how many tokens it has. – Eugene Jul 10 '13 at 09:24

6 Answers6

7

Try this:

int frequency = new StringTokenizer(myString, " ").countTokens();

For more details see StringTokenizer.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
2

Unfortunately, Scanner cannot do token counting without consuming these tokens from the input. So in order to access those tokens, you have to save them in a list:

List<String> tokens = new LinkedList<String>();
Scanner sc = new Scanner(System.in);
int count = 0;
while(sc.hasNext()) {
  tokens.add(sc.next());
  count++;
}
System.out.println("Number of tokens: "+count);
f1sh
  • 11,489
  • 3
  • 25
  • 51
0

You can use Matcher:

Pattern pattern = Pattern.compile(token);
Matcher matcher = pattern.matcher(s);
int count = 0;
// Check all occurrences
while (matcher.find()) {
    ++count;
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

You could manage it using the split method:

public static int getTokenCount(String input) {
    if (input == null) {
        return 0;
    }
    input = input.trim();
    if (input.isEmpty()) {
        return 0;
    }
    return input.split("\\s+").length;
}
sp00m
  • 47,968
  • 31
  • 142
  • 252
0

Use split(), it supports regex, unlike StringTokenizer.

int nbOfTokens = sc.nextLine().split(sc.delimiter().pattern()).length;
BaSsGaz
  • 666
  • 1
  • 18
  • 31
0
import java.io.*;
import java.util.*;
public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        String delims= "[ .,'!?_@]+";
        int length1=s.split(delims).length;

        System.out.println(length1);
        String[] tokens=s.split(delims);
        for(String token : tokens){
            System.out.println(token);

        }


        scan.close();
    }
}
Thisuri
  • 409
  • 4
  • 5
  • 1
    Can you please also explain the Code? As Code only answers, may solve the Problem, but do not help others to understand. – Tobias Theel Mar 03 '18 at 17:22