954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to ouput the tokenized string

import javax.swing.*;

public class Tokenize{
public static String s[] = new String[100];
public static void main (String args[]){
String ask = JOptionPane.showInputDialog("Enter:");
isAsk(ask);

}

public static void isAsk(String ask){

StringTokenizer st = new StringTokenizer(ask," ",false);

int input = Integer.parseInt(st.nextToken());

if(input<=10&&input>=0){
System.out.println(input);

}
}

}


Here is the sample:
Enter: 1 2 3

I want to output 2, but it only output 1. What will i have to do with my code so that i can output either 1 or 2 or 3. Thank you ahead for the help.

l_03
Light Poster
37 posts since Oct 2008
Reputation Points: 5
Solved Threads: 0
 

You need to loop your code to print all the tokens. Something like:

while(st.hasMoreTokens()){
    // parse every token OR print it OR do whatever
}

Since currently there is no loop, it exits after printing just the first token.

Also note, the StringTokenizer class is a legacy class and maintained just for compatability reasons, it's use si discouraged. Use String.split() instead.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You