File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding all the numbers in a string

 
nathan gibson
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what can i use to add all the numbers in this string token? i need to add them all so i can divide by numTrials and get an average.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the java.lang.Integer class for one parsing option. I'll reiterate my suggestion to check out Sun's Java tutorials, get a reference book, or search the web: when I used Google to search for "java +convert string to number" I get a *lot* of relevant hits on the first page.

(And consider a code indentation style; it makes things much, much easier to read.)
 
nathan gibson
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am familiar with converting strings to numbers, but that is not what i am trying to do, im trying to see if there is another method because i have to up the number of trial to 1000 and that is way to many variable to make.
 
nathan gibson
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also its pretty hard to search google for something if you dont know what something is, i did google, and all i got is string methods in the apis that werent what i was looking for.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another way. If you mean that each character in the string represents a separate digit, then you can isolate each character and treat it as an integer by using a simple arithmetic expression. Have a look at the following page, and pay particular attention to the DEC (for decimal) and Symbol columns.

http://www.ascii-code.com/

by your earlier post, consider using a loop, so you don't need a variable for each number.

http://java.sun.com/docs/books/tutorial/java/data/strings.html
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nathan gibson wrote:i am familiar with converting strings to numbers, but that is not what i am trying to do, im trying to see if there is another method because i have to up the number of trial to 1000 and that is way to many variable to make.

You said you were trying to add all the numbers in the string token--that implies converting each string representation of a number into a number and adding them. If the problem is something different, perhaps you could explain further. To get an average from the string tokens you just need to convert them to an integer in the loop you already have and add them, then divide, as you already stated.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nathan gibson wrote: i have to up the number of trial to 1000 and that is way to many variable to make.


Are you familiar with the concept of an array, or a List?
 
nathan gibson
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no im familiar with neither of these. we havent went over them yet.
 
nathan gibson
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ david. my question is though, will that work with something like a thousand trials. i was trying to find something that would basically work like average = sumToken / counter. if you think your way would be suitable would you please list an example of the code that i would need?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would work with a million trials: keep a sum, convert each token to an integer in the loop you already have, and add it to the sum. There's no need to create a new variable for each conversion--use the same one for the converted token, since you never need it again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic