Results 1 to 14 of 14
  1. #1
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default Multiply a number with the value In an ArrayList

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

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

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

    Huh?!
    Java Code:
    value = arraylist.get(index);
    int result = number * value;
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default 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. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

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

    Yep. Just iterate over all values then.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

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

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

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

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

    Thank you, Igor ;-)

    Phil
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default 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. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

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

    I suspect it's just a raw ArrayList, as the error refers to Object.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default 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));
                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();
         }
    
       }
    
    }

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

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

    Value should be a double. Set it by using Double.valueOf(prr.get(i));
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Default 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 ;                       
                             cp.add(nP);
                             System.out.println("it works " + cp);
            }
            return cp;
        }
    }

  13. #13
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

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

    Cast it to String, in your previous post prr was an ArrayList<String>
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  14. #14
    Paus34 is offline Member
    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

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

    thanks again :D

Similar Threads

  1. Replies: 17
    Last Post: 07-19-2011, 12:53 AM
  2. Replies: 37
    Last Post: 07-04-2011, 07:01 PM
  3. Find the index number of strings in an arraylist
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 03-24-2011, 03:25 PM
  4. matrix multiply
    By slixtrix in forum New To Java
    Replies: 8
    Last Post: 09-13-2010, 06:50 AM
  5. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:50 PM

Posting Permissions

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