-2

I have a problem with this source when I use StringTokenizer and Scanner together.

This is the source:

package deberes.contador;

import java.util.Scanner;
import java.util.StringTokenizer;

public class Contador {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Escriba una frase: ");
        String mensaje = scan.next();
        StringTokenizer palabras = new StringTokenizer(mensaje);
        int nPalabras = palabras.countTokens();
        System.out.println(nPalabras);
    }
}

The result of "nPalabras" is always "1", where is the problem?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Kemical
  • 7
  • 1
  • 5

1 Answers1

3

next() returns only one complete token. Try nextLine() if you have many tokens.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Mechkov
  • 4,294
  • 1
  • 17
  • 25