Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By jim829

Thread: Splitting a String While Keeping Some Spaces

  1. #1
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default Splitting a String While Keeping Some Spaces

    I am trying to split a string into a String[] tokens array to declare variables for an object; however, I'm having an issue getting the string to tokenize correctly. Here's an example of the input:

    a : 100 : John Smith : 20 Main St.
    a : 101 : Mary Jones : 32 Brook Rd.

    Here is the basic code I have now, to properly sort each line of text, etc. (without the split() method):
    Java Code:
    while (scanner.hasNextLine()) {
                    currentLine = scanner.nextLine();
                    lineScan = new Scanner(currentLine);
                    if (currentLine.startsWith("/") || currentLine.trim().isEmpty())
                        continue;
                    currentLine = currentLine.replace(':', '\0');
                    character = currentLine.charAt(0);
    
                    if (character == 'a') {
                        currentLine = currentLine.substring(2);
                        System.out.println(currentLine);
                    } else if (character == 'b') {
                        currentLine = currentLine.substring(2);
                        System.out.println(currentLine);
                    } else {
                        scanner.nextLine();
                    } 
    }
    I was able to eliminate the comments and identifiers from the text by trimming the first two characters of the string. For the split, I tried String[] tempArray = currentLine.split("\\s+"); however, that also took the spaces out of the addresses and names...so the results looked like this:

    100

    John
    Smith

    20
    Main
    St.

    As you can see, it splits via space regardless, including where I replaced all the :'s with spaces. Is there any way to do this? A better way?

    Any guidance is greatly appreciated.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Splitting a String While Keeping Some Spaces

    Why don't you replace " : " (space:space) with ":" and then use split(":") ?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Splitting a String While Keeping Some Spaces

    Uhm, why not just split("\\s*:\\s*") and ignore the first index (0) if you don't want that first bit?

    Edit: A bit slow posting!

  4. #4
    Cod
    Cod is offline Member
    Join Date
    Nov 2009
    Location
    Honolulu, HI
    Posts
    59
    Rep Power
    0

    Default Re: Splitting a String While Keeping Some Spaces

    Awesome, thanks for the guidance. I was able to adjust everything to print out perfectly for now. Here's the latest code (same snippet):

    Java Code:
                while (scanner.hasNextLine()) {
                    currentLine = scanner.nextLine();
                    lineScan = new Scanner(currentLine);
                    if (currentLine.startsWith("/") || currentLine.trim().isEmpty())
                        continue;
                    //currentLine = currentLine.replace(':', '\0');
                    character = currentLine.charAt(0);
    
                    if (character == 'a') {
                        currentLine = currentLine.substring(4);
                        tempAuthorArray = currentLine.split("\\s*:\\s*");
                        System.out.println(Arrays.toString(tempAuthorArray));
                        //System.out.println(currentLine);
                    } else if (character == 'b') {
                        currentLine = currentLine.substring(4);
                        tempBookArray = currentLine.split("\\s*:\\s*");
                        System.out.println(Arrays.toString(tempBookArray));
                        //System.out.println(currentLine);
                    } else {
                        scanner.nextLine();
                    }
                }
    Since the input is being read correctly, I know have to work on getting them input as Author and Book objects respectively. Then, into respective ArrayList in order to setup a "search" via GUI.

    Thanks again!

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Splitting a String While Keeping Some Spaces

    To add onto this thread, I have a question.

    I know that you can tokenize based on a character or set of characters. But can you tokenize based on !character?
    Like if you were to tokenize by looking for !space + : + !space (just theoretically). How could someone do that?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Splitting a String While Keeping Some Spaces

    You should probably start your own thread for this. In any event, the character ^ inverts a character class (inside of []) so the
    following would probably work: "[^ ]*:[^ ]*". Depending on how you want to parse it you may want to include a : in the character
    class as well.

    Regards,
    Jim
    Last edited by jim829; 01-21-2014 at 11:56 PM.
    AlexGraal likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Splitting a String While Keeping Some Spaces

    Thanks, that's all I needed. A small question - nothing thread-worthy

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Splitting a String While Keeping Some Spaces

    Quote Originally Posted by AlexGraal View Post
    Thanks, that's all I needed. A small question - nothing thread-worthy
    BUT, if you are truly looking for "not whitespace" then you can use \\S rather than \\s

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Splitting a String While Keeping Some Spaces

    Yeah. I thought of that. But I wanted to provide the option of adding a : in the character class. I probably should have mentioned that too.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Splitting the string[]
    By issoa in forum New To Java
    Replies: 1
    Last Post: 02-28-2012, 06:56 AM
  2. Java string splitting and concatination
    By mc1234 in forum New To Java
    Replies: 6
    Last Post: 09-26-2011, 03:02 PM
  3. Problem splitting a string
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-13-2011, 08:56 PM
  4. String Splitting
    By A.M.S in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 07:17 AM
  5. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •