Question

In JAVA please, Given any integer, print an English phrase that describes the integer (e.g. “One...

In JAVA please,

Given any integer, print an English phrase that describes the integer (e.g. “One Thousand, Two Hundred Thirty Four”). An ArrayList must be used in your program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if anything is required.

Copyable code:

import java.io.*;
import java.util.*;


public class Main
{
   public static void main(String[] args) {
      
       ArrayList <String> engPhrase = new ArrayList <String>(19);
      
       //ArrayList to store the english Phrase for numbers
       engPhrase.add("Zero");
       engPhrase.add("One");
       engPhrase.add("Two");
       engPhrase.add("Three");
       engPhrase.add("Four");
       engPhrase.add("Five");
       engPhrase.add("Six");
       engPhrase.add("Seven");
       engPhrase.add("Eight");
       engPhrase.add("Nine");
       engPhrase.add("Ten");
      
       engPhrase.add("Twenty");
       engPhrase.add("Thirty");
       engPhrase.add("Fourty");
       engPhrase.add("Fifty");
       engPhrase.add("Sixty");
       engPhrase.add("Seventy");
       engPhrase.add("Eighty");
       engPhrase.add("Ninety");
      
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter the number: "); //taking number input from the user
       int number = sc.nextInt();
       int temp = number;
      
       String strNumber = Integer.toString(number); //converting the number to string
      
       if(strNumber.length()==1) //if the number is of single digit
       {
       System.out.println(engPhrase.get(number));
       }
      
       else if(strNumber.length()==2) //if number is of two digits
       {
       //extracting the two digits from the number
       int ones= number%10;
       number=number/10;
       int tens= number%10;
      
       //printing the required results
       System.out.print(engPhrase.get(tens+9)+" ");
       if(ones!=0)
       System.out.print(engPhrase.get(ones)+" ");
       System.out.println("");
       }
      
       else if(strNumber.length()==3) //if number is of three digits
       {
       //extracting the three digits from the number
       int ones= number%10;
       number=number/10;
       int tens= number%10;
       number=number/10;
       int hundreds= number%10;
      
       //printing the required results
       System.out.print(engPhrase.get(hundreds)+" Hundred ");
       if(tens!=0)
       System.out.print(engPhrase.get(tens+9)+" ");
       if(ones!=0)
       System.out.print(engPhrase.get(ones)+" ");
       System.out.println("");
      
       }
      
       else if(strNumber.length()==4) //if number is of four digits
       {
       //extracting the four digits from the number
       int ones= number%10;
       number=number/10;
       int tens= number%10;
       number=number/10;
       int hundreds= number%10;
       number=number/10;
       int thousands= number%10;
      
       //printing the required results
       System.out.print(engPhrase.get(thousands)+" Thousand"+" ");
       if(hundreds!=0)
       System.out.print(engPhrase.get(hundreds)+" Hundred"+" ");
       if(tens!=0)
       System.out.print(engPhrase.get(tens+9)+" ");
       if(ones!=0)
       System.out.print(engPhrase.get(ones));
       System.out.println("");
       }
      
      
      
   }
}

Sample output1:

Sample output2:

Sample output3:

Sample output4:

Add a comment
Know the answer?
Add Answer to:
In JAVA please, Given any integer, print an English phrase that describes the integer (e.g. “One...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C program that converts digits into word form. It should accept one command line argument, which...

    C program that converts digits into word form. It should accept one command line argument, which is an integer in the range from -2147483648 to 2147483647. (This is the valid range of 32-bit values in C language.) The program will convert the given number into english words and display the answer exactly as in the shown below: This is C language only, Im having trouble getting passed 1 million and need help badly! thank you. zero one hundred and twenty-three...

  • Lab 2​​ ​CISC 1115 Start a new Java program. It should begin by printing, as part...

    Lab 2​​ ​CISC 1115 Start a new Java program. It should begin by printing, as part of the output (in addition to the comment at the beginning of your code, a header for the lab (or homework) that looks like the following: -------------------------------------------------------------------------- Your name​ Due Date CISC 1115 MY11 ​ Lab2 -------------------------------------------------------------------------- Write a program that will read a double from the keyboard, which represents a dollar and cents amount, such as 7325.67, and prints out: 7325.67 is 7...

  • Days Given the poem: Thirty days hath September April, June, and November All the rest have...

    Days Given the poem: Thirty days hath September April, June, and November All the rest have thirty-one with February's 28 to make it fun. Leap Year happening one in four, Gives February one day more. Write a program that does the following: 1. Prompts the user for a month string (January, February, ...). Validate the input month string against an array of month's, and prompt the user again if the String entered is not a valid month. Note: You may...

  • Please use the algorithm to generate 1000 random numbers in JAVA. (Print every 50th) integer array...

    Please use the algorithm to generate 1000 random numbers in JAVA. (Print every 50th) integer array lDon real array (aiim eo ← any integer such that l < 10 < 231-1 for i = 1 to n do 4 ← remainder when /.til IS divided by 2. 31-1 231 end for

  • Given the poem: Thirty days hath September April, June, and November All the rest have thirty-one...

    Given the poem: Thirty days hath September April, June, and November All the rest have thirty-one with February's 28 to make it fun. Leap Year happening one in four, Gives February one day more. How would I write this program using an Enum instead of an array? 1. Prompts the user for a month string (January, February, ...). Validate the input month string against an array of month's, and prompt the user again if the String entered is not a...

  • I need java code... Background Suppose you're working on the next great cligital assistant to compete...

    I need java code... Background Suppose you're working on the next great cligital assistant to compete with the likes of Siri, Cortana Alexa, and the lameless Google Assistant. An important part of your assistant is a text-10-speerlingine, You've managed to get your assistant to pronounce Englisli words, but you've hit a sag! The resistant is unable to pronounce numbers. When it sees 6243, it just says "six two four three insteud of the proper "six thousand two hundred forty three."...

  • Programming language: Java Design an application that has three classes. The first one, named Book describes...

    Programming language: Java Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...

  • PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first...

    PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first word which has its reverse word further ahead in the string. Input Format: A string of space separated words, ending with a 's'. Conditions ; Each string ends with a $ character. All characters in the string are either spaces. $ or lower case English alphabets, Output Format : In a single line pent either . The first word from the start of the...

  • answer in java Given a Binary Search Tree containing integer values, write a method to print...

    answer in java Given a Binary Search Tree containing integer values, write a method to print the values in descending order. public class Treenode { int val; Treenode left; Treenode right; Treenode (int x) { val = x; } The method signature is: public void reverseSorted(Treenode root){ // your code

  • i need help making this C++ code. Lab #2 Assignments: Numbers Class that translate whole dollar...

    i need help making this C++ code. Lab #2 Assignments: Numbers Class that translate whole dollar amounts in the range 0 through 9999 into an English description of the number. Numbers Class Design a class numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number. For example, the number 713 would be translated into the string seven hundred thirteen, and 8203 would be translated into eight thousand...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT