Reputation: 11
I have an array full of string, the array is going to be used to create a CSV file from the array elements, but before I dump the values into a CSV file I need to perform some multiplication on some elements. For example I need to multiply the value "20" in array[0] by another value say "100"in array[1].
Anyone have any tips on where to begin. I'm totally new to Java, so please go gentle on me.
Thanks
Upvotes: 1
Views: 138
Reputation: 86
To me, I would do the following:
int value = array[0] * array[1]
Not sure if your array is made up of integers or another type (say Strings, depends where you got the values from. If you needed to multiple specific values together, you could do something like this:
for (int = 0; i < 50; i++){
value = array[i] * array[i + 10]
Now that specific example would only work if you knew that you were going to multiple the xth position in the array, and then the 10th position after that. That being said, I'm sure you could use different loops to work out which two values needed multiplying together (as long as it's not just random, and your array has some form of logical structure to it).
Upvotes: 0
Reputation: 29281
Strings
can't be multiplied. They only get concatenated.
If you need to perform integer multiplication, you first need to cast String
to integer and then multiply.
Example Code
public static void main(String args[]) {
String[] arr = {"20", "30", "100"};
int num1 = Integer.parseInt(arr[0]);
int num2 = Integer.parseInt(arr[2]);
System.out.println(num1 * num2);
}
Output
2000
Upvotes: 2