This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

How to sum up elements of an arraylist having type String?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have an arraylist consisting of decimal numbers and i m supposed to take the average of last 4 elements of this arraylist.And these rational number are type of String.

private void average(String confidence) {

if(myList.size() >= 4) {

String t = myList.get(myList.size()-1);
String d = myList.get(myList.size()-2);
String f = myList.get(myList.size()-3);
String h = myList.get(myList.size()-4);

String s = (t + d + f+h) ;
long fin = Long.parseLong(s);
long result = fin/4 ;
System.out.println("average is: "+result);
}

but this method does not work.Could you please tell me what kind of changes am i supposed to do or any advices of doing this? Thanks a lot in advance!!!
 
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing out the variable "s".

You'll see why it's not working.

Also, why are you taking in values as Strings? Why not just take them in as doubles?
 
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, please UseCodeTags. Your code has all the right pieces, but not in exactly the right order. Adding strings is not the same as adding numbers. 2 + 2 is 4, but "2" + "2" is "22". What you need to do is convert each string to a number before adding them together.

Also, to get the average of four numbers you'll need to divide by 4, not 3. There's also the issue of integer vs. floating point arithmetic, but it may not be an issue for your assignment. I'd say to get the rest working, and then we'll talk about what's happening to the fractions if necessary.
 
Ziya Bakan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg Charles wrote:First, please UseCodeTags. Your code has all the right pieces, but not in exactly the right order. Adding strings is not the same as adding numbers. 2 + 2 is 4, but "2" + "2" is "22". What you need to do is convert each string to a number before adding them together.

Also, to get the average of four numbers you'll need to divide by 4, not 3. There's also the issue of integer vs. floating point arithmetic, but it may not be an issue for your assignment. I'd say to get the rest working, and then we'll talk about what's happening to the fractions if necessary.




if(myList.size() >= 4) {


Long t = Long.parseLong(myList.get(myList.size()-1));
Long d = Long.pareseLong(myList.get(myList.size()-2));
Long f = Long.parseLong(myList.get(myList.size()-3));
Long h = Long.parseLong(myList.get(myList.size()-4));

Long s = (t + d + f+h) ;
Long result = s/4 ;
System.out.println("meanAsrConfidence is: "+result);
}

i changed it like this but still does not work...one additional info os these elements of the arraylist are all rational numbers!!! ???



 
Ranch Hand
Posts: 2107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ziya Bakan wrote:still does not work...



Please tell the details. What is the error you received.
 
Greg Charles
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, please UseCodeTags.

Jesus is right that "still does not work" doesn't give us enough information to help you. I suspect that now you're hitting truncation problems because you're working with integers, and all your problems would be solved by working with Double instead of Long. I can't tell for sure though. Do the number strings in the array list contain decimal points? I'd already assumed they were all rational numbers. It's actually not possible to represent an irrational number as a string unless you allow using square roots or symbols like e or pi (or tau).
 
Bring out your dead! Or a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!