Samuel Synak
Samuel Synak

Reputation: 21

JAVA Convert 2d array of strings to 2d array of Number type

i have got little problem. I scanned numbers from .csv file into 2d array of strings. Now i need to parse it into 2d array of Number type. Not int or double. The error says:

Required type: com.sun.org.apache.xpath.internal.operations.Number Provided: java.lang.Number

Can you please take a look ?

 for (int a = 0; a < rows; a++) {
        for (int b = 0; b < cols; b++) {
            parsedContent[a][b] = NumberFormat.getInstance().parse(arrayOfStrings[a][b]);   
        }
 }

Upvotes: 0

Views: 38

Answers (1)

Steffi
Steffi

Reputation: 331

I have a small sample programm for you:

public static void main(String[] args) throws Exception{

        int rows = 2;
        int cols = 3;

        String[][] arrayOfStrings = {{"1.1","2.222222222","3.333"},{"4.44","5555","666"}};

        Number[][] parsedContent = new Number[rows][cols];

        NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);

        for (int a = 0; a < rows; a++) {
            for (int b = 0; b < cols; b++) {
                parsedContent[a][b] = numberFormat.parse(arrayOfStrings[a][b]);
                System.out.println(parsedContent[a][b]);
            }
        }
}

Outout:

1.1
2.222222222
3.333
4.44
5555
666

Is that what you mean? Otherwise I need more code details and some sample Strings.

Upvotes: 1

Related Questions