Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to convert INT to String.

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to convert INT to String.

    Hi yall.
    Im trying to convert Number into String
    For ex.

    I used 6 eggs out of the 12 for the cake

    -> I used six eggs out of the 12 for the cake

    Trying to convert the numbers (only 0~9)

    public Converter(String startingString)
    {
    original = new String(startingString);
    workingOnIt = new StringBuilder(startingString);
    }
    public void convert( )
    {
    int index;
    for(index =0; index < original.length(); index++)
    {

    }
    can anyone help.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to convert INT to String.

    What have you tried?

    Have you learned about arrays yet?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to convert INT to String.

    I tried

    int[] number = {1,2,3,4,5,6,7,8,9};
    if(original.length() == number)
    {


    }

    but i dont know how to replace them.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to convert INT to String.

    The array was to hold the Strings for the names of the numbers
    and the number was used to index into the array.
    For example the first element in the array would be "zero"
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Re: How to convert INT to String.

    Quote Originally Posted by wkqktlfj View Post
    I tried

    int[] number = {1,2,3,4,5,6,7,8,9};
    if(original.length() == number)
    {


    }

    but i dont know how to replace them.
    First, you need an array to map number to string, like this:
    String[] number = {"zero","one","two","three","four","five","six","seven","eight","nine"};

    And then you need to split your string to list of word. (token your string)
    String[] words = your_data.split(" ");

    After that you need to go through your words & check if there has any number from 0->9
    for(String word : words){
      //check word & replace here
    }
    Ok, by now you need to check if it's number & replace it.
    if(word.lengh() == 1){
       char c = word.charAt(0);
       if(c >= '0' && c <='9')
         return number[c - '0'];
    }

    Incase you know ascii code, you code compare like that:

    int charCode = word.charAt(0);
    if( charCode >= 48 && charCode <= 57){
       return number[charCode - 48];
    }

    I think all would be clearly now, you need to think about it again & again and do coding by your self.
    If you need any help, just post your code then I & others would happy to help you.

    Happy Coding

Similar Threads

  1. Can't convert string to int
    By Soorma in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2013, 04:49 PM
  2. [SOLVED] convert int to String
    By itispj in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2011, 10:34 PM
  3. [SOLVED] convert String to int
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: June 3rd, 2010, 03:05 AM
  4. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  5. Convert string to int?
    By connex in forum Java Theory & Questions
    Replies: 1
    Last Post: December 9th, 2009, 05:06 AM