views:

541

answers:

2

Hi All I am a newcomer here,

I am using the following code from an open source library (Matrix Toolkits for Java) which outputs the following matrix

  1000       1000                   5
     3          5  1.000000000000e+00

I am trying to do a String split that will return me 1000,1000,5

I tried using String[] parts = str.trim().split("\\s");

but it seems using the \s as a String Token is wrong, any idea what I should use instead?

thanks a lot!

public String toString() {
        // Output into coordinate format. Indices start from 1 instead of 0
        Formatter out = new Formatter();

        out.format("%10d %10d %19d\n", numRows, numColumns, Matrices
                .cardinality(this));

        for (MatrixEntry e : this)
            if (e.get() != 0)
                out.format("%10d %10d % .12e\n", e.row() + 1, e.column() + 1, e
                        .get());

        return out.toString();
    }
+2  A: 

You should split on any number of spaces, not just single ones. That is, add "+" to your regexp like this:

String[] parts = str.trim().split("\\s+");
Guðmundur H
Thanks for the quick reply!it works!
freshWoWer
+1  A: 

StringTokenizer should also be able to do what you want.

String s = "  1000       1000                   5";
java.util.StringTokenizer st = new java.util.StringTokenizer(s);
while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
}
Bombe
nice alternative solution as well thanks alot!
freshWoWer