-
I need to seperate 1 input line into 4.
My program directs me to have the user input 1 input line containing 4 rational numbers representing, length, width, thickness, and weight. They will be seperated by commas.
For example:
4, 4, .009, .0015
I then have to take those values and make various If statements to group them into the right category.
What I need help on is splitting the input line into 4 values. Is there some class, part of java.lang.object? Is it stringtokenizer?
ASAP please. THANKS!
-
you can use the stringtokenizer class or simple read the user input as a String and call its split method
Code:
String s = "4, 4, .009, .0015";
String separated[] = s.split(',');
that will also leave you withe the white space after each comma, just trim() each array element to remove them
-
 Originally Posted by chimps
you can use the stringtokenizer class or simple read the user input as a String and call its split method
Code:
String s = "4, 4, .009, .0015";
String separated[] = s.split(',');
that will also leave you withe the white space after each comma, just trim() each array element to remove them
If the OP has JDK 1.5 installed s/he can use the Scanner class for this as well:
http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html
And the String.split(...) method takes a String as parameter (regex String to be precise).
;')
-
like prometheuzz said, the split( String ) method takes a String, not a char.
Code:
String s = "4, 4, .009, .0015";
String separated[] = s.split( ", " );
I added a space btw, so that it's split up into:
separated[1] = "4", separated[2] = ".009", etc. instead of them being equal to " 4" and " .009"
-
I wouldn't garauntee on there being spaces after every comma. I'd split by just the comma then use trim() if a possible whitespace was an issue.
-
 Originally Posted by Phaelax
I wouldn't garauntee on there being spaces after every comma. I'd split by just the comma then use trim() if a possible whitespace was an issue.
yeah good call. 
Code:
String s = "4, 4, .009, .0015";
String separated[] = s.split( "," );
for (int i = 0; i < separated.length; i++) {
separated[i] = separated[i].trim();
}
Similar Threads
-
Replies: 0
Last Post: 08-08-2005, 09:46 AM
-
By Murray Foxcroft in forum Web
Replies: 5
Last Post: 11-02-2000, 02:42 AM
-
Replies: 1
Last Post: 04-19-2000, 12:13 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks