# Multiply a number with the value In an ArrayList

• 05-22-2013, 09:28 AM
Paus34
Multiply a number with the value In an ArrayList
How do i multiply a number with the values in an ArrayList
• 05-22-2013, 09:32 AM
PhHein
Re: Multiply a number with the value In an ArrayList
Huh?!
Code:

```value = arraylist.get(index); int result = number * value;```
• 05-22-2013, 10:48 AM
Paus34
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
• 05-22-2013, 11:10 AM
PhHein
Re: Multiply a number with the value In an ArrayList
Yep. Just iterate over all values then.
• 05-22-2013, 11:21 AM
JosAH
Re: Multiply a number with the value In an ArrayList
Quote:

Originally Posted by PhHein
Yep. Just iterate over all values then.

Einstein, you're a genius ;-)

Jos
• 05-22-2013, 11:49 AM
PhHein
Re: Multiply a number with the value In an ArrayList
Thank you, Igor ;-)

Phil
• 05-22-2013, 01:10 PM
Paus34
Re: Multiply a number with the value In an ArrayList
Here is what i have
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
• 05-22-2013, 02:36 PM
jim829
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
• 05-22-2013, 02:48 PM
Tolls
Re: Multiply a number with the value In an ArrayList
I suspect it's just a raw ArrayList, as the error refers to Object.
• 05-23-2013, 09:04 AM
Paus34
Re: Multiply a number with the value In an ArrayList
Here is my code i need help with the ArrayList
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));             prr.add(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();     }   } }```
• 05-23-2013, 09:16 AM
PhHein
Re: Multiply a number with the value In an ArrayList
Value should be a double. Set it by using Double.valueOf(prr.get(i));
• 05-23-2013, 09:35 AM
Paus34
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
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 ;                                              cp.add(nP);                         System.out.println("it works " + cp);         }         return cp;     } }```
• 05-23-2013, 09:55 AM
PhHein
Re: Multiply a number with the value In an ArrayList
Cast it to String, in your previous post prr was an ArrayList<String>
• 05-23-2013, 10:46 AM
Paus34
Re: Multiply a number with the value In an ArrayList
thanks again :D