# Thread: Multiply a number with the value In an ArrayList

1. Member
Join Date
Mar 2013
Posts
16
Rep Power
0

## Multiply a number with the value In an ArrayList

How do i multiply a number with the values in an ArrayList

2. ## Re: Multiply a number with the value In an ArrayList

Huh?!
Java Code:
```value = arraylist.get(index);
int result = number * value;```

3. Member
Join Date
Mar 2013
Posts
16
Rep Power
0

## Re: Multiply a number with the value In an ArrayList

i want to multiply all the values in the ArrayList with 1.14...i think that code is for one value

4. ## Re: Multiply a number with the value In an ArrayList

Yep. Just iterate over all values then.

5. ## Re: Multiply a number with the value In an ArrayList

Originally Posted by PhHein
Yep. Just iterate over all values then.
Einstein, you're a genius ;-)

Jos

6. ## Re: Multiply a number with the value In an ArrayList

Thank you, Igor ;-)

Phil

7. Member
Join Date
Mar 2013
Posts
16
Rep Power
0

## Re: Multiply a number with the value In an ArrayList

Here is what i have
Java Code:
```for (int i = 0; i < prr.size(); i++) {
value = prr.get(i);
int result = 1.14 * value;             //GET ERROR First Type double Second Type Object
}```
What do i have to do
i started Java a Week ago

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,749
Rep Power
10

## Re: Multiply a number with the value In an ArrayList

How did you populate the ArrayList. Apparently it contains types other than numbers.

Regards,
Jim

9. Moderator
Join Date
Apr 2009
Posts
13,132
Rep Power
23

## Re: Multiply a number with the value In an ArrayList

I suspect it's just a raw ArrayList, as the error refers to Object.

10. Member
Join Date
Mar 2013
Posts
16
Rep Power
0

## Re: Multiply a number with the value In an ArrayList

Here is my code i need help with the ArrayList
Java Code:
```import java.util.ArrayList;
import java.util.regex.Pattern;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

/**
*
* @author Jacques
*/
public class MetricClient extends DefaultHandler {

public static void main(String argv[]) {
final ArrayList<String> prr = new ArrayList<String> ();
try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {

boolean bprice = false;
private Object value;

public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {

System.out.println("Start Element :" + qName);

if (qName.equalsIgnoreCase("Price")) {
bprice = true;

}

}

public void endElement(String uri, String localName,
String qName) throws SAXException {

System.out.println("End Element :" + qName);

}

public void characters(char ch[], int start, int length) throws SAXException {

if (bprice) {
//System.out.println("Price : " + new String(ch, start, length));
//System.out.println(prr);

bprice = false;
}

}

public void endDocument() throws SAXException {
System.out.println("Here we should send to webservice "+prr);
ArrayList cp = new ArrayList();
for (int i = 0; i < prr.size(); i++) {
value = prr.get(i);
value= 1.14 * value;     //HERE IS THE ERROR
System.out.println(value);
}

}
};

saxParser.parse("C:\\Webservices\\MetricClient\\src\\MetricClient\\quote.xml", handler);

} catch (Exception e) {
e.printStackTrace();
}

}

}```

11. ## Re: Multiply a number with the value In an ArrayList

Value should be a double. Set it by using Double.valueOf(prr.get(i));

12. Member
Join Date
Mar 2013
Posts
16
Rep Power
0

## Re: Multiply a number with the value In an ArrayList

ok i have done it and it works to test on the client side but im gonna use that FOR LOOP on the WEB service side so when i copied the FOR LOOP and set the varibles correct i get an ERROR .This is the WEB service side code
Java Code:
```import java.util.ArrayList;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
*
* @author Jacques
*/
@WebService(serviceName = "MetricService")
public class MetricService {

/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}

/**
* Web service operation
*/
@WebMethod(operationName = "takeArray")
public ArrayList takeArray(@WebParam(name = "array1") ArrayList array1) {
ArrayList cp = new ArrayList();
for (int i = 0; i < array1.size(); i++) {
double nP = Double.valueOf(array1.get(i));  //method Double.valueOf(double) is not applicable (actual argument Object cannot be converted to double by method invocation conversion)
nP = nP * 2 ;
System.out.println("it works " + cp);
}
return cp;
}
}```

13. ## Re: Multiply a number with the value In an ArrayList

Cast it to String, in your previous post prr was an ArrayList<String>

14. Member
Join Date
Mar 2013
Posts
16
Rep Power
0

## Re: Multiply a number with the value In an ArrayList

thanks again :D

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•