简体   繁体   中英

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]);   
        }
 }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM