multiplying all elements in an array by an outside number?

Posted by prodo on Stack Overflow See other posts from Stack Overflow or by prodo
Published on 2013-10-26T15:41:46Z Indexed on 2013/10/26 15:53 UTC
Read the original article Hit count: 119

Filed under:
|
|

I need to multiple all the values in an array by 3000 which in turn would create a new array that I will use to subtract from another array. I've tried to create a separate method that would do that for me but all I got back in the multiplied array was a bunch of numbers and symbols strangely?

here is the code that I wrote

public static void main(String[] args)
{    
    int numberOfTaxpayers = Integer.parseInt(JOptionPane.showInputDialog("Enter how many users you would like to calculate taxes for: ");
    int[] usernumChild = new int[numberOfTaxPayers];
    for (int i = 0; i < usernumChild.length; i++)
    {
        usernumChild[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter number of children for user "+ (i+1) +": "));
    }//this for loop finds out the number of children per user so we can later multiply each input by 3000 to create an array that determine dependency exemption for each user
int[] depndExemp = multiply(usernumChild, 3000);//this was the calling of the multiply method... somewhere here is the error!!
}//end main method 
public static int[] multiply(int[] children, int number)
{
    int array[] = new int[children.length];
    for( int i = 0; i < children.length; i++)
    {
       children[i] = children[i] * number;
    }//end for
    return array;
}//this is the method that I was shown in a previous post on how to create return an array in this the dependency exemption array but when I tested this by printing out the dependency array all I received were a jumble of wrong numbers.

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays