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

Thread: How to convert a digit to words

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How to convert a digit to words

    I am trying to figure out how to convert digits from 0 to 9 to words in my program.
    What is the proper code to do this?

    I have found examples online but none of them are working with my code.
    What is just a generic example? I need to do it inside a switch statement too.

    switch (operation)
            {
                case "+":
                    System.out.println("The sum of ");
            }

    I need to take my input of numbers from the user as digits and change them to words in this system.out statement.


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

    Default Re: How to convert a digit to words

    convert digits from 0 to 9 to words
    If the digits are int values, make an array of the Strings that correspond to the int values (eg 0 = "zero")
    and use the int value to index into the array to get the corresponding String.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Changing digits to words

    import java.util.*;
     
    public class StudentInformation
    { 
        public static void main (String [] args)
        {
            Scanner scan = new Scanner (System.in);
     
            System.out.println("Please enter the first number between 0 and 9: ");
            int num1 = scan.nextInt();
     
            System.out.println("\nPlease enter the second number between 0 and 9: ");
            int num2 = scan.nextInt();
     
            System.out.println("\nPlease enter your operator: + for ADDITION, - for SUBTRACTION, * for MULTIPLICATION,");
            System.out.println("/ for DIVISION, or ^ for EXPONENTIATION: ");
            String operation = scan.next();
     
            if (num1 < 0 || num1 > 9 || num2 < 0 || num2 > 9)
            {
                System.out.println("\nInvalid Number");
            }
     
            NumberToWords num = new NumberToWords();
     
            switch (operation)
            {
                case "+":
                    System.out.println("The sum of " + num.convert(num1) + " and " + num.convert(num2) + " is " + (num1 + num2));
                    break;
                case "-":
                    System.out.println("The difference of ");
                    break;
                case "*":
                    System.out.println("The product of ");
                    break;
                case "/":
                    System.out.println("The quotient of ");
                    break;
                case "^":
                    System.out.println("The expontentiation of ");
                    break;
                default:
                    System.out.println(operation + " is not valid.");
            }
     
        }
    }

    I am not sure how to write an array and have looked it up. Where is it supposed to be located at in my program? I need to convert the user input integers into words for the system.out statements in the switch loop.
    Thanks

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

    Default Re: How to convert a digit to words

    Where is it supposed to be located at in my program?
    If everything in the program is in the main() method, you can put it in the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Changing digits to words

    Quote Originally Posted by Spanky_10 View Post
     
            if (num1 < 0 || num1 > 9 || num2 < 0 || num2 > 9)
            {
                System.out.println("\nInvalid Number");
            }
    This is not too valid to your question, but think about moving this if() statement, you have it after you imput both the first and the second number, so the user is not sure which number they imputed incorrectly. You could always make two if you need to.

    To write an array you have to first declare it:

    int[] myIntArray = new int[3];
    // This will give you an array that holds integers. 
    // The 3 means that it will hold three values, you can then assign your values: 
     
    myIntArray[0] = 1;
    myIntArray[1] = 2;
    myIntArray[2] = 3;
    // This will then assign the values 1,2,3 to the array positions 0,1,2
    // So when you do:
     
    System.out.print(myIntArray[0]);
    //You will get 1.

    Try and think about how you can use this to get words.
    This is just a basic way of declaring arrays.
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!

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

    Default Re: How to convert a digit to words

    Topic continued at: http://www.javaprogrammingforums.com...e-program.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  2. Digit counter
    By Rexshine in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 16th, 2013, 06:53 PM
  3. checking for digit while reading from a file
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: July 27th, 2012, 03:51 PM
  4. Timer digit
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 2nd, 2011, 08:07 PM
  5. Trying to crunch a date to a single digit
    By twitch09 in forum Java Theory & Questions
    Replies: 7
    Last Post: October 15th, 2010, 12:14 AM