This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String > Integer

 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello.

this is another question about strings (not another by me but another from yet another newbie). i have three strings (one,two,three) which have values between one and nine. have do i get the average for them, as anytime i try to divide by 3 it wont run. ive tried messing around with integers and floats. my code is as follows:

String Average;
Average = String.valueof(one+two+three);
textbox.setText(average);

the result which is shown in the text box is on fact the three strings repeated i.e. instead of (9+3+6)/3 showing 6, it shows 936.

thankyouin advance :-)
 
Liam Tiarnach
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by joseph mcgratton:
hello.

this is another question about strings (not another by me but another from yet another newbie). i have three strings (one,two,three) which have values between one and nine. have do i get the average for them, as anytime i try to divide by 3 it wont run. ive tried messing around with integers and floats. my code is as follows:

String Average;
Average = String.valueof(one+two+three);
textbox.setText(average);

the result which is shown in the text box is on fact the three strings repeated i.e. instead of (9+3+6)/3 showing 6, it shows 936.

thankyouin advance :-)

This might be pretty ugly... but...

it works, is this what you are looking for ???
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it sounds like the code is doing exactly what it ought to. look up the Java documentation for String.valueOf(); it appears this method doesn't actually do what you perhaps think it does.

as a hint towards doing what you want to get done, you'll need to convert the strings to integers first. there are static methods in the Integer class that will do this for you.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to numerically calculate something, then you need to use numeric types.

String representations of digits can be easily converted to numeric types -- generally using methods in the wrapper classes (e.g., Integer). But to convert words into numeric values, you'll need to write your own code.

(Or am I misunderstanding what you're trying to do?)
[ February 15, 2005: Message edited by: marc weber ]
 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!

thanks all for your fast replies - much appreciated. some1 mentioned i had to convert to integers. i thought i had to do that but dont know how.
for the people who dont fully understand my question, i am creating three random numbers, which are displayed in text boxes. *i want to display the average of the three random numbers within another textbox*. my example of 9 3 6 was just an easy exapmle = the 3 numbers can be anything. they are strings, which cannot be used to get an average. so like my title said i need help on how to convert strings to integers! :-) help could include keywords to google search for. ive tried strings,integers,java division etc, but to now avail! im sure i will crack it soon enuff but id rather spend 1 hour than 5 hrs doing it hehe! thanks again for replies.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this, and look around the page for the "Integer" class. click on that, and dig in.
 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much for your help - ive added that page to favorites. i think i got what im looking for - parseint.

thanks for your time :-)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc just gave you all the keywords you need:
String representations of digits can be easily converted to numeric types -- generally using methods in the wrapper classes (e.g., Integer).

But before googling these and sorting through a hundred results, look at the Java API (javadocs)
http://java.sun.com/j2se/1.4.2/docs/api/index.html

Specifically at java.lang.Integer
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html
or, if you want more accurate numbers, java.lang.Double:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html

The javadocs show us that parseDouble takes a String as it's only argument and returns a primive 'double' type. It's static so we don't need to instanciate a Double object to use it. We will however need to catch or throw a NumberFormatException in the event that what's in your textField is not a valid number.

Now you know how to convert each string to something you can add and divide with. When you're done with your math you'll want to convert the value back to a String for display purposes. Let's look at the API for String:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

String has a version of valueOf for every primitive type and object.
This should do it:



I laid it all out to make it as simple as possible. You'll probably want to inline the parseDouble methods to avoid having to create the double variables. (the ugly ones with the underscores).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic