Marios Filipidis
Marios Filipidis

Reputation: 35

How can i split the user's input with tokens?

i need your advise in order to do something ... i want to take the user's input line as i already do in my program with scanner ... but i want to split each command(word) to tokens .... i do not know how to do that , till now i was playing with substring but if the users for example press twice the space-bar button everything is wrong !!!!!

For example :
Please insert a command : I am 20 years old

with substring or .split(" ") , it runs but think about having :

Please insert a command : I am 20 years old

That is why i need your advice .... The question is how can i split the user's input with tokens.

Upvotes: 1

Views: 2910

Answers (3)

Andremoniy
Andremoniy

Reputation: 34900

Well, you need to normalize you string line before splitting it to tokens. A simplest way is to remove repeated whitespace characters:

line = line.replaceAll("\\s+", " ");

(this will also replace all tabs to a single " ").

Upvotes: 6

Digitalwolf
Digitalwolf

Reputation: 493

Okay, so the code that I have made is going to ask you for to insert a command, it is then going to take your command and then split it around any number of spaces (due to the Regex). It then saves these words, numbers, speech marks etc. as tokens. You can then manipulate each word in the for loop.

import java.util.Scanner;

public class CommandReaderProgram{ 

  public static void main(String[] args){
    Scanner userInput = new Scanner(System.in);        
    System.out.println("Please insert a command:");
    String temp = userInput.nextLine();

        while(temp != null){
            String [] tokens = temp.split("\\s+");
            for(String word : tokens){
                System.out.print(word);
                System.out.print(" ");    
            } 
            break;    
        }   
        System.out.print("\n");
 }
}

To test that each word, number and speech mark has actually been saved as a token, change the character in the System.out.print(" "); code to anything. e.g System.out.print(""); or System.out.print("abcdefg"); and it will put this data between each token, to prove that the tokens are indeed separate.

Unfortunately I am unable to call the token array outside of the for loop at the moment, but will let you know when I figure it out.

I'd like to hear what type of program you are trying to make as I think we are both trying to make something very similar.

Hope this is what you are looking for.

Regards.

Upvotes: 0

saisrinivas
saisrinivas

Reputation: 64

Use the StringTokenizer class. From the API :

"[It] allows an application to break a string into tokens... A StringTokenizer object internally maintains a current position within the string to be tokenized."

The following sample code from the API will give you an idea:

    StringTokenizer st = new StringTokenizer("this is a test");
    while (st.hasMoreTokens()) {
        System.out.println(st.nextToken());
    }

It produces the following result:

    this
    is
    a
    test

Upvotes: 3

Related Questions