Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Changing 8 to eight...need help!

    EDIT: About the confusion. I want to try and create a program that takes the input of "8" or any given number like "12,526" and returns to you "eight" or "twelve thousand five hundred twenty-six." and visa versa, but that can come later
    Have made no progress so far, don't really know where to start. This is a personal project of mine.

    What I am thinking is just making a main string with a Scanner that will take the input of the number and run it through a bunch of if statements and then return something.

    EDIT2: http://mindprod.com/applet/inwords.html
    that link will help you grasp a sense of what I want to accomplish if you still don't understand it

    Don't know where to start, or how to go about doing this. Some help as an outline or something would be greatly appreciated
    Last edited by adjit; 06-18-2011 at 07:41 AM.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Please note that title are NOT included or serves as a your question.
    Please explain what you are trying to do, what have you done so far.

  3. #3
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    Sorry, just included an EDIT in the main post

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Is you converter accepts numbers with decimal entry? If yes, how long a decimal digit can get OR will you limit it to two decimal only?
    Like 1587.98 is allowed but 1587.987 is not allowed?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Don't know where to start
    I don't know precisely what you mean by this. If you are saying that you don't know the basics of how to write a small Java program - defining a class and its methods, using basic control of flow constructs like loops and if/else - then the best thing to do is to put your immediate aim aside and go through a good Tutorial. Oracle offers The Java (tm) Tutorials, but there are many others in books or online.

    On the other hand if it is this specific task of converting between numerals with digits and separators and words for the same number, then the place to start is by figuring out how you - rather than a computer - would convert between "12,526" and "twelve thousand five hundred twenty-six". You need to be able to specify the recipe completely and precisely in order to know what it is that you are going to write as code.

    Look for patterns that reoccur like the "twelve" that is part of "12,526" and "1,012".

    Work on simpler variants of the problem first. Start with converting between "1"->"9" and the corresponding words. Then extend that up to "99", perhaps by dealing with each digit separately.

  6. #6
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    Well, I know how I would do simple numbers.
    But do I have the right idea with the scanner and input 9 and have it print out nine?
    Also, wouldn't I have to write essentially an infinite code in order to get it to work properly? because there can be an infinite number of inputs

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No, you can either use recursion or perform it iteratively, both ways a switch case will be helpful. Maybe it will be helpful to first split the numbers up without other words.
    12562 would be twelve five sixty two.

    Decide whether you would like to do this iteratively or recursively. Then think how you would pronounce the following numbers: 2, 20, 200, 2000, besides 20, what is the difference between these pronunciations? Would you append something to the changing part to pronounce the words correctly? Switch cases will be helpful here, the code will be very large, and there can be an infinite amount of code if you want(because there is an infinite amount of numbers).

    For example, if you want to do numbers over a million, you need to handle millions, for billions you have to handle billion, etc. I suggest you start small and expand as you go.
    Last edited by sunde887; 06-18-2011 at 09:05 AM.

  8. #8
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    I'm relatively new to Java, took a 201 course, but I don't know what recursion is, so if you could point me in the right direction, it would be helpful

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Formatting input into something like 100,000,000.00 will be helpful because you can use split() method. Then
    you can use a for loop and check if you are converting hundred, thousand, million, billions and so on...

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    I answered this question before (including all code!); a forum search may cough it up.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    I thought split() was used on buffered files like a text file? or could you also use it for the input of something into the scanner?
    also, would there be a way to have it so if I were to put in a number like 1,245 or 1245 (without the comma) that it would come out the same even if the split() method would be in place?

  12. #12
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I answered this question before (including all code!); a forum search may cough it up.

    kind regards,

    Jos
    could you possibly give me something to go by for the search? or even a link please.
    But also I want to do this on my own, so I understand it instead of just copying a previously posted code

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you don't know what recursion is, you can either learn it with this problem, or stick to a string builder and iterative approach.

    However, I'll give you a quick run down of what recursion is, there is a lot of material out there on the subject, and a book called "How to Design Programs"(freely available at htdp.org) is exceptionally good at helping you understand it(it doesn't use java however).

    Recursion is a way of defining a method in terms of itself. For example the definition(in words) of summing an array recursively is
    1. If the array is empty, return 0
    2. The first item in the array + the method performed on the rest of the array

    Arrays don't lend themselves perfectly to this definition since there is no easy way to get the rest of an array, but you can very easily keep track of the index currently worked on.

    Java Code:
    public int sumArray(int index, int[] array){
      if(index==array.length)
        return 0;
      else{
        return array[index] + sumArray(index+1, array);
      }
    }
    public int sumArray(int[] array){
      sumArray(0, array);
    }
    This method adds the terms in an array recursively. It basically breaks the problem into pieces and adds them together with recursive calls. This expands on the stack every time the method calls itself again.

    I'll show an easier method to help show how the method looks on the stack

    The factorial of a number n is defined as
    1. If n is 1, return 1
    2. n times factorial of n - 1

    In both of these example step 1 is a termination condition, you set up termination conditions to end the program, you must carefully have correct termination conditions or you will get infinite recursion and a stack overflow exception.

    Java Code:
    public int factorial(int n){
      if(n==1)
        return 1;
      else{
        return n * factorial(n - 1);
      }
    }
    This code will run until it reaches the recursive call, then it will add another method call to the stack. It looks like this as the program moves forward
    Java Code:
    factorial(5)
    5 * factorial(4)
    5 * 4 * factorial(3)
    5 * 4 * 3 * factorial(2)
    5 * 4 * 3 * 2 * factorial(1) --termination condition reached
    5 * 4 * 3 * 2 * 1
    5 * 4 * 3 * 2
    5 * 4 * 6
    5 * 24
    120

  14. #14
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    No, you need to format the input so you can use split. If you did not do it the
    return will still 1234.
    See this
    Using Predefined Formats (The Java™ Tutorials > Internationalization > Formatting)
    Customizing Formats (The Java™ Tutorials > Internationalization > Formatting)

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by adjit View Post
    could you possibly give me something to go by for the search? or even a link please.
    But also I want to do this on my own, so I understand it instead of just copying a previously posted code
    Suppose you want to transform the number xyz (where x, y, and z are digits) to words: note that you can do x-hundred-yz where you have to transform the (smaller) numbers x and yz to words; similar with the number xyztuv, it breaks down to xyz-thousand-t-hunderd-uv, etc. etc. The number zero is bit of an exception to this rule.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by sunde887 View Post
    The factorial of a number n is defined as
    1. If n is 1, return 1
    2. n times factorial of n - 1
    The pitiful mathematician in me must protest in the most strongliest way here:

    The factorial of a number n is defined as
    1. If n is 0, return 1
    2. n times factorial of n - 1

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    [QUOTE=sunde887;216144]
    Java Code:
    public int sumArray(int index, int[] array){
      if(index==array.length)
        return 0;
      else{
        return array[index] + sumArray(index+1, array);
      }
    }
    public int sumArray(int[] array){
      sumArray(0, array);
    }
    So I kind of understand that, but I don't get the point of
    Java Code:
     int[] array
    in both methods. What is that doing?

  18. #18
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Suppose you want to transform the number xyz (where x, y, and z are digits) to words: note that you can do x-hundred-yz where you have to transform the (smaller) numbers x and yz to words; similar with the number xyztuv, it breaks down to xyz-thousand-t-hunderd-uv, etc. etc. The number zero is bit of an exception to this rule.

    kind regards,

    Jos
    So would I write this code by saying something that would say:
    Java Code:
    if(the input number has 3 numbers){
    System.out.println("hundred")
    }
    else if(the input number has 4 numbers){
    System.out.println("thousand")
    }
    should it look something like that in the end?
    Also, how would I break it down beyond that?

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by adjit View Post
    So would I write this code by saying something that would say:
    Java Code:
    if(the input number has 3 numbers){
    System.out.println("hundred")
    }
    else if(the input number has 4 numbers){
    System.out.println("thousand")
    }
    should it look something like that in the end?
    Also, how would I break it down beyond that?
    Yep, something like that; think of the modulo operator (%), e.g.

    Java Code:
    int xyz= 123;
    int x= xyz/100;
    int yx= xyz%100;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Yep, something like that; think of the modulo operator (%), e.g.

    Java Code:
    int xyz= 123;
    int x= xyz/100;
    int yx= xyz%100;
    kind regards,

    Jos
    I kind of don't get that. What's the point of the modulo? Don't understand why you do xyz/100 and yx%100

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  2. help changing the RGB of a fillRect
    By AnimeKitty in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 06:02 PM
  3. Changing Cursor
    By ridvan in forum AWT / Swing
    Replies: 2
    Last Post: 06-27-2010, 04:11 AM
  4. Changing the values of a map.
    By Onra in forum New To Java
    Replies: 1
    Last Post: 02-26-2010, 12:20 AM
  5. Changing the Jframe
    By Nemo1959 in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 03:58 PM

Posting Permissions

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