Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-26-2008, 07:02 PM
Member
 
Join Date: Nov 2007
Posts: 46
carderne is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance Issues (StringTokenizer) JavaForums Java Blogs 0 11-29-2007 03:22 PM
How to use StringTokenizer for multiple tokens javaplus New To Java 2 11-29-2007 11:38 AM
StringTokenizer Java Tip Java Tips 0 11-08-2007 10:48 AM
StringTokenizer Java Tip Java Tips 0 11-03-2007 11:24 PM


All times are GMT +3. The time now is 02:36 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org