This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes adding all the numbers in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "adding all the numbers in a string" Watch "adding all the numbers in a string" New topic
Author

adding all the numbers in a string

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120


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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 16, 2009
Posts: 120
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

Joined: Sep 16, 2009
Posts: 120
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

Joined: May 13, 2009
Posts: 679
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
no im familiar with neither of these. we havent went over them yet.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
@ 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

Joined: Sep 29, 2008
Posts: 12617

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: adding all the numbers in a string
 
Similar Threads
program to calculate male to female ratio of a given number
flipping coin program
guessing game
when i run my virtual machine just keeps going, but no output.
simple simple jave game