Results 1 to 15 of 15

Thread: Split string

  1. #1
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default Split string

    Hello,

    I need to split a string like the following example:

    word1 | word2 | word3 | word4 : word5 : word6

    I am splitting the string into an array as follows:

    Java Code:
    String[] words = input.nextLine().split("[|:]");

    I'm finding that before word2 to word6 there is a space, and I can't figure out why! :

    word1
    _word2
    _word3
    _word4
    _word5
    _word6

    Can someone please help?
    Last edited by codeAJ; 05-08-2011 at 03:17 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Since you are trying to remove the spaces as well, add the leading space to your split argument.
    [ | :] works for me in a quick test.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Or add optional white space before and after the [|:], e.g.,

    Java Code:
    String[] words = test.split("\\s+[|:]\\s+");

  4. #4
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Or add optional white space before and after the [|:], e.g.,

    Java Code:
    String[] words = test.split("\\s+[|:]\\s+");
    Many thanks, this worked a treat. :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by codeAJ View Post
    Many thanks, this worked a treat. :)
    I'm sorry, but it's wrong. It should be:
    Java Code:
    String[] words = test.split("\\s*[|:]\\s*");
    The one I did before, the white space wasn't optional.

  6. #6
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm sorry, but it's wrong. It should be:
    Java Code:
    String[] words = test.split("\\s*[|:]\\s*");
    The one I did before, the white space wasn't optional.

    Thanks for your help.

    When I print these strings to screen with System.out.println they are fine.

    However when they are passed as arguments to an object's constructor they are being saved as 'null'! :confused:

    Java Code:
    User user = new User(words[0], words[1], words[2]);

    Any ideas on what may be causing this problem?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    They shouldn't be null, mind showing some of your code?

  8. #8
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    They shouldn't be null, mind showing some of your code?

    I have extracted out the following code:


    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class Words {
    
       public static void main(String[] args) {
    
          Scanner input = new Scanner(new File("words.txt"));
          while (input.hasNext()) {
             String[] words = input.nextLine().split("\\s*[|:]\\s*");
             System.out.println(words[0]);  // Prints ok
             System.out.println(words[1]);  // Prints ok
             User user = new User(words[0], words[1]);
             user.printDetails(); // Prints wrong. e.g. Word1: null 
          }
       }
    }
    
    public class User {
    
       private String word1, word2;
    
       public User(String word1, String word2) {
          this.word1 = word1;
          this.word2 = word2;
       }
    
       public void printDetails() {
    
          System.out.println("Word1: " + word1);
          System.out.println("Word2: " + word2);
       }
    }

    Has me beat!

  9. #9
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Please ignore my previous post.

    I had made mistakenly overridden the User's instance variables.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What change did you make to fix it? The code you posted looks good.

  11. #11
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    What change did you make to fix it? The code you posted looks good.

    My previous post of code was only a snippet of what I thought was relevant.

    User extends another class, and being a newbie I made the mistake of declaring the private instance variables again in the subclass.

    So I removed these declarations and updated the print method:

    public void printDetails() {

    System.out.println("Word1: " + super.getWord1);
    System.out.println("Word2: " + super.getWord2);
    }

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad you solved it. Please mark your solved with the thread tools at the top.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    The super keyword is only needed to invoke the superclass's implementation of a method that's overridden in the subclass. This doesn't appear to be the case here.

    db

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by DarrylBurke View Post
    The super keyword is only needed to invoke the superclass's implementation of a method that's overridden in the subclass. This doesn't appear to be the case here.

    db
    Good point, didnt even notice that. Op, if getWord is in the super class as public it is inherited by the subclass making the call to super unnecessary as Darryl stated.

  15. #15
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Thanks for the tips. I have removed the "super." as this public method is indeed inherited. :D

Similar Threads

  1. String split help
    By MiddleBlocker in forum New To Java
    Replies: 4
    Last Post: 03-11-2011, 03:57 AM
  2. String split help
    By YoungJavaBoy in forum New To Java
    Replies: 7
    Last Post: 01-19-2011, 02:39 AM
  3. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 05:08 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 09:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:32 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
  •