Thanks for the help in advance. So basically I have some Strings (lets call these Strings "Numbers") that looks like this.

Numbers1 = "   3.74343E-01   4.82245E-01   1.95635E-01  -1.05565E-01   3.81702E-02"
Numbers2 = "   2.23965E-01   1.29925E-01   4.08791E-02  -4.21790E-02  -1.21410E-01"
Numbers3 = "  -2.73593E-01   8.63407E-02  -1.37682E-01  -3.01382E-01  -1.51439E-01"

My program is supposed to take this string, split each piece into an individual String, and put each one in an array. So basically I need my outputted array to look like this:

StoredStrings1[] = ["3.74343E-01", "4.82245E-01", "1.95635E-01", "-1.05565E-01", "3.81702E-02"]
StoredStrings2[] = ["2.23965E-01", "1.29925E-01", "4.08791E-02", "-4.21790E-02", "-1.21410E-01"]
StoredStrings3[] = ["-2.73593E-01", "8.63407E-02", "-1.37682E-01", "-3.01382E-01", "-1.51439E-01"]

Now, what I would usually do is something like this:

StoredStrings1 = Numbers1.split("   ");
StoredStrings2 = Numbers2.split("   ");
StoredStrings3 = Numbers3.split("   ");

This way, the "Numbers" would be split based on the three spaces between each number. However, there's a slight problem. Because some of the numbers have a negative sign, the spaces in these cases are only 2 between. So basically, there's spots with 2 spaces and spots with 3 spaces between, and I do not know how I could make sure my program splits all the strings accordingly, because the split method does not work. Any help with splitting the strings all the same would be deeply appreciated.

Recommended Answers

All 5 Replies

the split method does not work

Can you show what happened and explain why it was wrong?

The split() method takes a reg expression. Try making one to skip 1 to n spaces.

You want to split on any amount of whitespace? Try
StoredStrings1 = Numbers1.split("\\s+");

\s matches [ \t\n\x0B\f\r], so it should suit your purposes.
Or if you want to be more specific, you can match on just one or more spaces:
StoredStrings1 = Numbers1.split(" +")

You're going to want to read up on regular expressions at some stage if you want to get much more complicated than just using the canned classes. That can get a little tricky, but it's worth knowing.

You want to split on any amount of whitespace? Try
StoredStrings1 = Numbers1.split("\\s+");

\s matches [ \t\n\x0B\f\r], so it should suit your purposes.
Or if you want to be more specific, you can match on just one or more spaces:
StoredStrings1 = Numbers1.split(" +")

You're going to want to read up on regular expressions at some stage if you want to get much more complicated than just using the canned classes. That can get a little tricky, but it's worth knowing.

Ah, I see. so I just split with " +" as the parameters. That is very good to know! I will read up on expressions one of these days, because they really seem to come in handy for some of the things I need to do. thanks for your response! I will try this right now.

Just to clarify, that's "[space]+" - not "+"!

The poor old StringTokenizer might work:

StringTokenizer st = new StringTokenizer(Numbers1, " ");
   splitNumbers = new String[st.countTokens()];
   for(int i=0; i < splitNumbers.length; i++)  {
     splitNumbers[i] = st.nextToken();
   }
   System.out.println("split=" + Arrays.toString(splitNumbers));

split=[3.74343E-01, 4.82245E-01, 1.95635E-01, -1.05565E-01, 3.81702E-02]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.