How to ouput the tokenized string

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 37
Reputation: l_03 has a little shameless behaviour in the past 
Solved Threads: 0
l_03 l_03 is offline Offline
Light Poster

How to ouput the tokenized string

 
0
  #1
Feb 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: How to ouput the tokenized string

 
0
  #2
Feb 17th, 2009
You need to loop your code to print all the tokens. Something like:
  1. while(st.hasMoreTokens()){
  2. // parse every token OR print it OR do whatever
  3. }
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC