943,899 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 316
  • Java RSS
Feb 17th, 2009
0

How to ouput the tokenized string

Expand Post »
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.
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Feb 17th, 2009
0

Re: How to ouput the tokenized string

You need to loop your code to print all the tokens. Something like:
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with For Loop
Next Thread in Java Forum Timeline: program for reading from .csv file shows errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC