Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

String to int & back again

Thread view: 
fb - 21 Jan 2006 02:18 GMT
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.

Signature

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.

klynn47@comcast.net - 21 Jan 2006 02:57 GMT
Well Strings are immutable so you can can't them. However, you can
change what String the String reference myString refers to.
Allan Bruce - 21 Jan 2006 04:33 GMT
> Hello.  I have the following code:
>
[quoted text clipped - 11 lines]
>
> 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
Roedy Green - 21 Jan 2006 04:48 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

IchBin - 21 Jan 2006 05:03 GMT
> Hello.  I have the following code:
>
[quoted text clipped - 11 lines]
>
> 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-)
Hal Rosser - 22 Jan 2006 01:43 GMT
> Hello.  I have the following code:
>
[quoted text clipped - 9 lines]
> 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.
Ian Mills - 22 Jan 2006 12:49 GMT
> Hello.  I have the following code:
>
[quoted text clipped - 11 lines]
>
> ttyl.

Try

    float num = 205.0f;
    int myInt = 0;
    for(int i=0;i<=5;i++){
        g2D.drawString("" + myInt,70.0f,num);
        num-=20.0f;
        myInt+=10;
    }
fb - 23 Jan 2006 01:05 GMT
> Hello.  I have the following code:

<Snip Code>

Thanks everyone.  They were all very helpful answers.

fb
Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.