AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String to int & back again

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
fb
Guest





PostPosted: Sat Jan 21, 2006 2:18 am    Post subject: String to int & back again Reply with quote



Hello. I have the following code:

float num = 205.0f;
String myString = "0";
for(int i=0;i<=5;i++){
g2D.drawString(myString,70.0f,num);
num-=20.0f;
myString+=10;
}

I am looking to increase the value of myString by 10. However, since it
is a string I can't really increment it...(Can I?), So I was wondering
If there is a simple way to achieve this. (Or any way at all really).

ttyl.

--
Pro'-gram 1) n. A magical spell cast over a computer which transforms
user input into error messages. 2) vt. An activity similar to banging
one's head against a wall, but with less opportunity for relief.
Back to top
klynn47@comcast.net
Guest





PostPosted: Sat Jan 21, 2006 2:57 am    Post subject: Re: String to int & back again Reply with quote



Well Strings are immutable so you can can't them. However, you can
change what String the String reference myString refers to.

Back to top
Allan Bruce
Guest





PostPosted: Sat Jan 21, 2006 4:33 am    Post subject: Re: String to int & back again Reply with quote




"fb" <fb (AT) noway (DOT) com> wrote

Quote:
Hello. I have the following code:

float num = 205.0f;
String myString = "0";
for(int i=0;i<=5;i++){
g2D.drawString(myString,70.0f,num);
num-=20.0f;
myString+=10;
}

I am looking to increase the value of myString by 10. However, since it
is a string I can't really increment it...(Can I?), So I was wondering If
there is a simple way to achieve this. (Or any way at all really).

ttyl.


You can do something like this:

int myNum = 0;
String myString = "" + myNum;

// do things and increment myNum

myString = "" + myNum;


Alternatively you can convert from String to int by:
int myInt = Integer.parseInt(myString);

HTH
Allan



Back to top
Roedy Green
Guest





PostPosted: Sat Jan 21, 2006 4:48 am    Post subject: Re: String to int & back again Reply with quote

On Sat, 21 Jan 2006 02:18:54 GMT, fb <fb (AT) noway (DOT) com> wrote, quoted or
indirectly quoted someone who said :

Quote:

I am looking to increase the value of myString by 10. However, since it
is a string I can't really increment it...(Can I?), So I was wondering
If there is a simple way to achieve this. (Or any way at all really).

see http://mindprod.com/applet/converter.html

for String > int and int > String.

If you find yourself doing this often, you might decide to keep the
int value around and only generate the String when it comes time to
play.

This thinking goes way back to COBOL COMPUTATIONAL-n variables.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Back to top
IchBin
Guest





PostPosted: Sat Jan 21, 2006 5:03 am    Post subject: Re: String to int & back again Reply with quote

fb wrote:
Quote:
Hello. I have the following code:

float num = 205.0f;
String myString = "0";
for(int i=0;i<=5;i++){
g2D.drawString(myString,70.0f,num);
num-=20.0f;
myString+=10;
}

I am looking to increase the value of myString by 10. However, since it
is a string I can't really increment it...(Can I?), So I was wondering
If there is a simple way to achieve this. (Or any way at all really).

ttyl.

How about looking at it the other way around..


float num = 205.0f;
int myInt = 0;

for(int i=0;i<=5;i++)
{
g2D.drawString(Integer.toString(myInt),70.0f,num);
num-=20.0f;
myInt+=10;
}

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

Back to top
Hal Rosser
Guest





PostPosted: Sun Jan 22, 2006 1:43 am    Post subject: Re: String to int & back again Reply with quote


"fb" <fb (AT) noway (DOT) com> wrote

Quote:
Hello. I have the following code:

float num = 205.0f;
String myString = "0";
for(int i=0;i<=5;i++){
g2D.drawString(myString,70.0f,num);
num-=20.0f;
myString+=10;
}

I am looking to increase the value of myString by 10. However, since it
is a string I can't really increment it...(Can I?), So I was wondering
If there is a simple way to achieve this. (Or any way at all really).

If you need to increment myString so often, then maybe you should consider

using an int in the first place - and cast it to a string when you need it.




Back to top
fb
Guest





PostPosted: Mon Jan 23, 2006 1:05 am    Post subject: Re: String to int & back again Reply with quote

fb wrote:
Quote:
Hello. I have the following code:

Snip Code


Thanks everyone. They were all very helpful answers.

fb
--
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too
dark to read anyway." - Groucho Marx

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.