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 4 of 4

Thread: NumberWords

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NumberWords

    Hello, I am trying to create a program that will convert any number from 1 to 999 from its digit representation to its word representation. But I cannot figure out how to display it after the user has entered it. Here is what I have so far:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package numberwords1v1;
    import java.io.*;
     
     
    /**
     *
     * 
     */
    public class NumberWords1V1 {
     
        /**
         * @param args the command line arguments
         */
        public static void main (String args []) throws IOException {
     
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); //user input
            System.out.println("Numbers to Words (1-999 Edition)");
            System.out.println("================================ ");
            int tryAgain = 1; //will initiate the first do-while loop of the program.
            do {
                System.out.println();
                System.out.println("Input a number (1-999) and this program will repeat it to you with words.");
                double numInput = Integer.parseInt(br.readLine()); //user inputs number
                System.out.println();
     
                int error = 1; //will initiate error check
     
                do {
                    if (numInput < 1 || numInput > 999) {//if the user enters an invalid number
                        System.out.println("Invalid number.  Enter a number between 1 and 999.");
                        numInput = Integer.parseInt(br.readLine()); //user must re-enter a number if they entered an invalid one
                        System.out.println();}
                    else {
                        error = 0;} //will end error loop if the number is correct
                } while (error == 1);
     
                System.out.println();
     
                double hundredsDigit = Math.floor(numInput);
                double tensDigit = Math.floor((numInput % 100) / 10);
                double onesDigit = numInput % 10;
     
                System.out.print("Your number in words is: " );
     
                if ((numInput >= 1) && (numInput <= 9)) {//will initiate the ones subroutine
                    ones(onesDigit);}
     
                if ((numInput >= 10) && (numInput <= 19)) {//will initiate the teens subroutine
                    teens(numInput);}
     
                if ((numInput >= 20) && (numInput <= 99)) {//will initiate the tens and ones subroutine
                    tens(tensDigit);
                    System.out.print(" "); //puts a space between the tens and ones 
                    ones(onesDigit);}
     
                if ((numInput >= 100) && (numInput <= 999)) {//will initiate the hundreds, tens and ones subroutine
                    hundreds(hundredsDigit);
                    System.out.print(" "); //puts a space between the hundreds and tens
                    tens(tensDigit);
                    System.out.print(" "); //puts a space between the tens and ones
                    ones(onesDigit);
                }
                    System.out.println();
                    System.out.println();
                    System.out.println("Press 1 to try again.");
                    tryAgain = Integer.parseInt(br.readLine()); //user decides to try again
                    System.out.println();
     
              } while (tryAgain == 1); //will repeat program if user chooses to enter another number
     
              } //closes main body
     
            public static void hundreds(double hundredsDigit){
     
                } //closes hundreds method
     
            public static void tens(double tensDigit){
     
              } //closes tens method
     
            public static void teens(double numInput){
     
              } //closes teens method    
     
            public static void ones(double onesDigit) {
     
              }//closes ones method
     
     
        }

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: NumberWords

    But I cannot figure out how to display it after the user has entered it
    You should define what you mean by display it...how do you want to display it? You use System.out.println several times in the code, so I presume you know how to display it that way which makes me confused as to the question

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NumberWords

    When I try to run the code in a scanner method, I type the number in but it doesn't show the number in words.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: NumberWords

    Quote Originally Posted by Pettsa View Post
    When I try to run the code in a scanner method, I type the number in but it doesn't show the number in words.
    I'm confused...is your question how you can do this, or why your code doesn't do this? Because you have several empty methods that do nothing