0

I'm having trouble multiplying the elements of a 2d string array by the element of another. Heres the code:

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

Here are what the files look like:

array:
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

prices array:
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

As you can see, the first column of each array lists the city, so if the cities match up, I need the 1st array's elements multiplied by omaha(7.5).

stinepike
  • 54,068
  • 14
  • 92
  • 112
user2906074
  • 55
  • 5
  • 11
  • convert the string into floating numbers.... also check if you are getting proper value in arrays while parsing the file – stinepike Feb 04 '14 at 05:09

4 Answers4

1

IF you have no choice but to use Strings where numbers should be the best choice, then try to convert your strings to numbers like this -

String str = "22.43";
try{
    double str = Double.parseDouble(str);
}catch(NumberFormatException nfe){
    nfe.printStackTrace(); 
}

This is just an example. You might want to see the disadvantages of using print stack trace - Why is exception.printStackTrace() considered bad practice?

Community
  • 1
  • 1
Erran Morad
  • 4,563
  • 10
  • 43
  • 72
0

Try casting the values to either an integer or double type. Currently you are attempting to multiply string values.

JNeether
  • 25
  • 3
0

There are two problems with your inner loop.

1) You need to increment 'i' at some point. You need to have in your loop:

i++;

Currently, you are traversing the rows not the columns in your inner loop, get rid of the:

row++;

2) You never initialize the columns of newArray. Since you are using a static 2D array you should have a max number of columns shown here as MAX_WIDTH.

String [][] newArray = new String[array.length][MAX_WIDTH];

As the others mentioned, you definitely need to convert from String to double before you can do any 'math' on the variables. Here Java automatically casts Double (class) to double (data structure).

try {
    double val = Double.parseDouble(string);
} catch (NumberFormatException e) {
    e.printStackTrace();
}
donkon
  • 909
  • 9
  • 23
0

You are multiplying Strings, first convert them to double and then multiply First you are not returning any thing and also Change your return type from String to String [] [] as you are returning 2d Array

Here is the code

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

Your inner loop should be something like I have done in the code depending on what you want

Hamza
  • 1,593
  • 2
  • 19
  • 31