I need to seperate 1 input line into 4.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: I need to seperate 1 input line into 4.

  1. #1
    Join Date
    Dec 2005
    Posts
    4

    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!

  2. #2
    Join Date
    Aug 2005
    Posts
    43
    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

  3. #3
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote 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).
    ;')

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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"

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    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.

  6. #6
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote 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

  1. Replies: 0
    Last Post: 08-08-2005, 10:46 AM
  2. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 03:42 AM
  3. Command line input
    By Basu in forum Java
    Replies: 1
    Last Post: 04-19-2000, 01: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center