CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 1999
    Posts
    1

    How to typecast/convert an integer to a string and back????



    Hi to anyone reading this message.


    I started learning and programming in Java a couple of days ago and I already have a problem. This is the kind of problem that is (probably) very easy to answer, and demonstrates my inexperience of Java, but here goes.

    I am using Asymetrix Supercede Java Edition version 1.0 as my Java compiler (not my first choice, but it's all I have at the moment), and I needed to convert a string into an integer. I have found a solution by taking each character of the string and type casting it as an (int) and adding it to an integer variable (mulitplied by the position in the string (eg. if the string is "1234" the integer would be ( (int)('1')(*1000) + (int)('2')(*100) + (int)('3')(*10) + (int)('4') ) ) but this doesn't seem like a very efficient way of doing it. I have looked in the help files and there seems to be other ways to do it, but none seem to be accepted by the compiler. Can anyone give me any suggestions and examples????

    Also, after some calculations, I need to convert the string back to an integer or long. The toInteger() and toLong() functions don't seem to work. Can someone offer help for this aswell????

    Many thanks in advance for any help given.

    I'm going to upgrade to Microsoft Visual J++ soon.

    Mark.


    Check out the FTR web page here

  2. #2
    Join Date
    Mar 1999
    Posts
    8

    Re: How to typecast/convert an integer to a string and back????



    from String to int

    try

    {

    int yourInt = Integer.parseInt( yourString.trim());

    }

    catch( NumberFormatException nfe)

    {

    //.........

    }




    from int to String

    String yourString = "" + yourInt;

  3. #3
    Join Date
    Apr 1999
    Posts
    2

    Re: How to typecast/convert an integer to a string and back????



    Thanks for answering so quickly. It seems to work nicely.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured