Results 1 to 2 of 2

Thread: StringTokenizer

  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default StringTokenizer

    I have a form, where the user will enter a series of numbers, and need to make it so that the numbers are separated by spaces, and then then each number is stored as an int.

    Help with the StringTokenizer, or a better method would be much appreciated.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Use this method

    Hello

    I created a method that you can use. It uses vectors and auto boxing. ;)
    Java Code:
    import java.util.*;
    public class Main{
    	public static void main(String[] arg){
    		int[] numbers =  getNumbers("1 2 3 4 5");
    		for (int number : numbers){
    			System.out.println(number);
    		}
    	}
    	public static int[] getNumbers(String input){
    		Vector<Integer> list = new Vector<Integer>();
    		StringTokenizer tokens = new StringTokenizer(input);
    		while (tokens.hasMoreTokens())
    			list.add(new Integer(Integer.parseInt(tokens.nextToken())));
    		int[] result = new int[list.size()];
    		for (int i = 0; i < list.size(); i++) 
    			result[i] = list.get(i);
    		return result;
    	}
    }
    Hope that helped. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. How to use StringTokenizer for multiple tokens
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-29-2007, 09:38 AM
  2. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:48 AM
  3. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •