Seperating items within a string

Discussion in 'Programming General' started by DtheK, Nov 28, 2009.

Seperating items within a string
  1. Unread #1 - Nov 28, 2009 at 11:45 PM
  2. DtheK
    Joined:
    Oct 17, 2009
    Posts:
    873
    Referrals:
    0
    Sythe Gold:
    0

    DtheK Apprentice
    Banned

    Seperating items within a string

    Hi,

    I just wanted to know if there was a way to seperate items within a string, say, with a space in between each different input.

    For example,

    in the string "16 15" if I wanted to store 16 in one variable and 15 in another how would I go about doing this
     
  3. Unread #2 - Nov 28, 2009 at 11:57 PM
  4. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Seperating items within a string

    Create two different fields?
    Or an array?
    Or a collection?

    You'll need to provide some code for the most effective way to do this.
     
  5. Unread #3 - Nov 29, 2009 at 6:14 AM
  6. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

  7. Unread #4 - Nov 29, 2009 at 9:25 AM
  8. DtheK
    Joined:
    Oct 17, 2009
    Posts:
    873
    Referrals:
    0
    Sythe Gold:
    0

    DtheK Apprentice
    Banned

    Seperating items within a string

    Okay, here's some sample code:

    import javax.swing.JOptionPane;
    public class test
    {
    public static void main(String[] args)
    {
    String s = JOptionPane.showInputDialog("Enter the data with a space in between each number");
    }
    }


    Say I put "1 2 3 4" as the input and wanted to store 1 in variable a, 2 in variable b, etc.
     
  9. Unread #5 - Nov 29, 2009 at 10:43 AM
  10. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Seperating items within a string

    Then use the split() method, as SuF stated.
     
  11. Unread #6 - Nov 29, 2009 at 6:51 PM
  12. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Seperating items within a string

    ^That would store the input in a string array. Then you have to cast it to int or float or what ever and make sure it all works and discard any input that doesn't...
     
  13. Unread #7 - Nov 29, 2009 at 9:50 PM
  14. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Seperating items within a string

    Cast the java.lang.String[] or a primitive type? >_<

    I'm sure what you meant was, that you need to get a single java.lang.String element of the array, and call the corresponding method of the corresponding wrapper class to get a primitive type from the String (Object's cannot be cast to primitive types, as primitive types are not Objects).

    In pseudo-code
    Code:
    String input = JOptionPane.showInputDialog(...);
    if(input != null) {
        String[] data = input.split(" ");
        for(String s : data) {
            try {
                int i = Integer.parseInt(s);
                //Do whatever you need with the input
            } catch (RuntimeException e) { //I cbf to lookup the actual name
                //Execption-handling code here
            }
        }
    }
     
  15. Unread #8 - Nov 29, 2009 at 9:52 PM
  16. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Seperating items within a string

    ^Basically what I ment.
     
< A few questions.. | Information For Web Page Designers and Web Site Development >

Users viewing this thread
1 guest


 
 
Adblock breaks this site