aspose file tools*
The moose likes Beginning Java and the fly likes String > Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String > Integer" Watch "String > Integer" New topic
Author

String > Integer

joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
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

Joined: Aug 06, 2004
Posts: 51
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 ???


- Liam...<br />- ' He who never sleeps... '
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
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

Joined: Jan 14, 2005
Posts: 323
try this, and look around the page for the "Integer" class. click on that, and dig in.
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
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

Joined: Dec 11, 2004
Posts: 13410

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).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String > Integer