Thread: StringTokenizer
View Single Post
  #2 (permalink)  
Old 01-26-2008, 10:19 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Use this method
Hello

I created a method that you can use. It uses vectors and auto boxing.
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.
__________________
If your ship has not come in yet then build a lighthouse.
Reply With Quote