Ok, well I am now working on a new program where a program that counts the number of times a digit appears in a telephone number. And I have no idea how to even start this. Can anyone please help? Atleast try to give me a outline or something.

Heres what I have to do,

"
Write a program that counts the number of times a digit appears in a telephone number.  

Your program should begin by creating an array of size 10 that will hold the count for each digit from 0 to 9.  Next, read a telephone number from the keyboard.  The telephone number should be stored in a String variable.  Examine each character in the phone number, and increment the appropriate count in the array.  Display the contents of the array.

Note, the user might enter only numbers, as in: 2525551212
or they might enter dashes between the digits, as in: 252-555-1212
or they might enter parentheses and dashes, as in: (252)555-1212
or they might enter parentheses, dashes and spaces, as in: (252) 555-1212


Your program should be able to handle all of these possibilities.  

Hint: Ignore any character that isn't a digit.
Hint: Use one of the Character methods to determine if a character is a digit or not.


For example:


Please enter a 10-digit telephone number: 252-555-1212

The following is the number of times each digit appears:
0 - 0
1 - 2
2 - 4
3 - 0
4 - 0
5 - 4
6 - 0
7 - 0
8 - 0
9 - 0
"

Please someone help me please.

Recommended Answers

All 7 Replies

And I have no idea how to even start this.

Yes you do. It's all here:

Your program should begin by creating an array of size 10 that will hold the count for each digit from 0 to 9. Next, read a telephone number from the keyboard. The telephone number should be stored in a String variable. Examine each character in the phone number, and increment the appropriate count in the array. Display the contents of the array.

Do those things, in that order.

Break the problem down into steps:
1 - Create an array
2 - Prompt user for number
3 - Store the number as a string
4 - Either trim the non-digits out or figure out how you are going to ignore them
5 - Read each digit and increment the proper element of the array(probably with a loop)

From there, all you have to do is translate it into code. I assume that you have seen the methods and concepts that are needed to accomplish the steps, otherwise it wouldn't have been assigned. Good Luck.

commented: Exactly. +1

Here you are the JAVA program

    int telephoneNumberArrayIndex = 0;
    char[] telephoneNumberArray = new char[10];
    String telephoneNumber;
    Scanner s = new Scanner(System.in);
    System.out.print("Please enter a 10-digit telephone number: ");
    telephoneNumber = s.nextLine();
    System.out.println("\nThe following is the number of times each digit appears:");
    for (int i = 0; i < telephoneNumber.length(); i++) {
        if (telephoneNumber.charAt(i) >= (char) 48
                && telephoneNumber.charAt(i) <= (char) 57) {
            telephoneNumberArray[telephoneNumberArrayIndex++] = telephoneNumber.charAt(i);
        }
    }
    int[] counter = new int[10];
    for (int i = 0; i < counter.length; i++) {
        counter[i] = 0;
    }
    for (int i = 0; i < telephoneNumberArray.length; i++) {
        for (int j = 0; j < 10; j++) {
            if (telephoneNumberArray[i] == (char) (j + 48)) {
                counter[j]++;
            }
        }
    }
    for (int i = 0; i < telephoneNumberArray.length; i++) {
        System.out.println(i + " - " + counter[i]);
    }
commented: Do you really think this helps someone learn to program? +0
commented: dont give away the answers -1

naief, perhaps your intention was good, but the purpose of the forum is to help and guide people toward the solution - just giving the answer is not the proper way to go. Writing bits of code and by that help in places of struggle is always welcome - but to simply put the code is bad conduct.

naief, perhaps your intention was good, but the purpose of the forum is to help and guide people toward the solution - just giving the answer is not the proper way to go. Writing bits of code and by that help in places of struggle is always welcome - but to simply put the code is bad conduct.

well, I'm new here
If this is the purpose of this forum I will try my best

well, I'm new here
If this is the purpose of this forum I will try my best

Glad to have you here.

Glad to have you here.

Thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.