Results 1 to 15 of 15
Thread: Split string
- 05-08-2011, 02:14 AM #1
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 02:17 AM.
- 05-08-2011, 02:25 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
-
Or add optional white space before and after the [|:], e.g.,
Java Code:String[] words = test.split("\\s+[|:]\\s+");
- 05-08-2011, 02:51 AM #4
-
- 05-08-2011, 04:06 AM #6
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?
- 05-08-2011, 04:16 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
They shouldn't be null, mind showing some of your code?
- 05-08-2011, 05:22 AM #8
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!
- 05-08-2011, 05:28 AM #9
Please ignore my previous post.
I had made mistakenly overridden the User's instance variables.
- 05-08-2011, 05:30 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What change did you make to fix it? The code you posted looks good.
- 05-08-2011, 05:39 AM #11
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);
}
- 05-08-2011, 05:46 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Glad you solved it. Please mark your solved with the thread tools at the top.
- 05-08-2011, 06:20 AM #13
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
- 05-08-2011, 06:35 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
- 05-08-2011, 10:51 AM #15
Similar Threads
-
String split help
By MiddleBlocker in forum New To JavaReplies: 4Last Post: 03-11-2011, 02:57 AM -
String split help
By YoungJavaBoy in forum New To JavaReplies: 7Last Post: 01-19-2011, 01:39 AM -
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks