使用接收两个2d字符串数组的方法更新2d String数组

时间:2014-02-04 07:42:10

标签: java arrays

我必须接受一个2d数组,并用另一个相应的2d数组将它的每一行多个。

以下是文件:

Omaha,104,1218,418,216,438,618,274,234,510,538,740,540
Saint Louis,72,1006,392,686,626,670,204,286,236,344,394,930
Des Moines,116,1226,476,330,444,464,366,230,602,260,518,692
Chicago,408,948,80,472,626,290,372,282,488,456,376,580
Kansas City,308,1210,450,234,616,414,500,330,486,214,638,586
Austin,500,812,226,470,388,488,512,254,210,388,738,686
Houston,454,1086,430,616,356,534,218,420,494,382,476,846
New Orleans,304,1278,352,598,288,228,532,418,314,496,616,882

文件二:

Omaha,7.5
Saint Louis,10.5
Des Moines,8.5
Chicago,11.5
Kansas City,12.5
Austin,10.75
Houston,12.5
New Orleans,9.25

示例:当我将array[0][0]price[0][0]进行比较时,字符串匹配,因此我必须采用array[0]的整个行,并将每个元素乘以price[0][1]的元素以更新阵列。

现在这是我的代码:

public static String [][] updateString(String[][] array, String[][] prices)
    {
        String [][] newArray = new String[array.length][]; 
        for(int row = 0; row < array.length; row++)
        {
            if (array[row][0].equals(prices[row][0]))
            {
                for(int i = 0; i<array.length; i++)
                {
                    Double d=Double.parseDouble(array[row][i+1]) * Double.parseDouble(prices[row][1]);
                    newArray[row][i+1] = d.toString();
                } 
            }

        }

        return newArray;
    }

以下是我的错误:

Exception in thread "main" java.lang.NullPointerException
at assign_1.DansUtilities.updateString(DansUtilities.java:430)
at assign_1.SalesReportGenerator.main(SalesReportGenerator.java:50)

**第430行是我的方法。第50行是我称之为的地方。

新代码:

public static String [] [] updateString(String [] [] array,String [] [] price)         {

        for(int row = 0; row < array.length; row++)
        {
            if (array[row][0].equals(prices[row][0]))
            {
                for(int i = 0; i<array[row].length; i++)
                {
                   {Double d=Double.parseDouble(array[row][i]) * Double.parseDouble(prices[row][1]);
                    array[row][i] = d.toString();}
                } 
            }

        }

        return array;

继承我的新错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Omaha"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at assign_1.DansUtilities.updateString(DansUtilities.java:429)
at assign_1.SalesReportGenerator.main(SalesReportGenerator.java:50)

2 个答案:

答案 0 :(得分:0)

只有在数组为null时才能抛出NullPointerException。检查调用该方法的代码。

改进代码的其他说明:

  1. 您不需要新数组来存储值。你可以在现有的数组中完成它。
  2. 你的内部for循环必须从1到当前行的长度而不是数组的长度所以它应该是这样的:

    for(int i = 1; i&lt; array [row] .length; i ++) 并在循环内容中使用index i而不是i + 1.

答案 1 :(得分:0)

我建议您创建字符串的输出,以查看它们是否从文件中正确读取。

    private static void outputString2D(String[][] s, String name) {
        for ( int i = 0; i < s.length; i++) {
            for ( int j = 0; j < s[i].length; j++ ) {
                System.out.println(name + " contains at [" + i + "][" + j + "]:\t" + s[i][j]);
            }
        }

    }

我如何使用它的示例:

    public static void main(String[] args) {

    String[][] str = new String[2][3];

    str[0][0] = new String("I am 0,0.");
    str[0][1] = new String("I am 0,1.");
    str[0][2] = new String("I am 0,2.");
    str[1][0] = new String("I am 1,0.");
    str[1][1] = new String("I am 1,1.");
    str[1][2] = new String("I am 1,2.");

    outputString2D(str, "str");
}

示例输出:

str contains at [0][0]: I am 0,0.
str contains at [0][1]: I am 0,1.
str contains at [0][2]: I am 0,2.
str contains at [1][0]: I am 1,0.
str contains at [1][1]: I am 1,1.
str contains at [1][2]: I am 1,2.

请告诉我们您的两个字符串的内容。