• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

adding all the numbers in a string

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no im familiar with neither of these. we havent went over them yet.
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Happily living in the valley of the dried frogs with a few tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic