Question

Please write a java program that takes a large, user-entered number (usually 10 digits) and tallies...

Please write a java program that takes a large, user-entered number (usually 10 digits) and tallies the 10 digits to be used in an array. Then, the method should tell the user how many of each number was input.

For example, for the number 445903218, the program should say "2 4's, 1 5...," etc etc.

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

JAVA CODE:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] count = new int[10];
        int idx;
        System.out.print("Enter a large number: ");
        String number = scan.next(); // Read a number
        for (int i = 0; i < number.length(); i++) {
            idx = number.charAt(i) - '0'; // Get index whose count has to be incremented
            count[idx]++;
        }
        boolean isFirst = true;
        // Display count of each digit
        for (int i = 0; i < 10; i++) {
            if (count[i] == 1) { // If count is 1 then don't print 's at the end
                if (isFirst) { // Don't print comma if it is printed for the first time
                    System.out.print(count[i] + " " + i);
                    isFirst = false;
                } else
                    System.out.print(", " + count[i] + " " + i);
            } else if (count[i] > 1) {
                if (isFirst) { // Don't print comma if it is printed for the first time
                    System.out.print(count[i] + " " + i + "'s");
                    isFirst = false;
                } else
                    System.out.print(", " + count[i] + " " + i + "'s");
            }
        }
        scan.close();
    }
}

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Please write a java program that takes a large, user-entered number (usually 10 digits) and tallies...
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
  • (Java Please) Sum of Digits Write a program that will sum the digits of a number...

    (Java Please) Sum of Digits Write a program that will sum the digits of a number input by the user. For example, if the user enters the number 1234, the sum of the digits will be 10 (1 + 2 + 3 + 4 = 10). The user will enter a number with at least 4 digits (greater than 1000). That value will be sent to a method which will return the sum. Sample Output 1: SUM OF DIGITS -------------...

  • java Write an application that input a number from the user and checks if all digits...

    java Write an application that input a number from the user and checks if all digits are prime numbers using method prime. The number entered can be of any size. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers don’t use an if to check numbers from 1 – 9; you need to find an algorithm to check...

  • Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...

    Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • With your partner, brainstorm a program that will ask the user for a number of digits...

    With your partner, brainstorm a program that will ask the user for a number of digits to be stored. The program should then create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user. Create a new Project named FillArray and a new class in the default packaged named FillArray.java. You and your partner should...

  • Please write a java program that takes in user input of various numbers as a type...

    Please write a java program that takes in user input of various numbers as a type string, prints the sum of all numbers entered, and prints the smallest and largest numbers. Arrays are optional as they have not been covered by my professor yet. Thank you

  • Write a program named ClassifyScores that classifies a series of scores entered by the user into...

    Write a program named ClassifyScores that classifies a series of scores entered by the user into ranges (0-9, 10-19, and so forth). The scores will be numbers between 0 and 100. Design and implement a program to prompt the user to enter each score separately. If the score is within the correct range, then increment the appropriate range If the score entered is greater than 100, display an error message, ignore the incorrect score, and prompt for the user to...

  • A Java Problem. E5.19 Write a program that takes user input describing a playing card in...

    A Java Problem. E5.19 Write a program that takes user input describing a playing card in the following shorthand notation: А Ace 2... 10 Card values Jack Queen King Diamonds Hearts Spades Clubs Your program should print the full description of the card. For example, Enter the card notation: QS Queen of Spades Implement a class Card whose constructor takes the card notation string and whose getDescription method returns a description of the card. If the notation string is not...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • Create a program that takes in user input and stores the input into an array. Also...

    Create a program that takes in user input and stores the input into an array. Also within the program show how you can: show the elements in the array, remove an element from the array, add an element to the array. Do NOT ask the user how many elements they want to add, instead once they are done adding, they should hit ‘q’ and it should break out of the input. This is a c++ program. The input type shall...

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
ADVERTISEMENT