3

My assignment is to write a program that counts the number of times a digit is repeated in a phone number (numbers only). This is what I have so far and its output.

import java.util.Scanner;

public class PhoneNumber {

  public static void main(String[] args) {         
    Scanner key= new Scanner(System.in);        
    String number;        
    int[] phoneArray = new int[10];      
    int one = 0;       
    System.out.println("Enter your phone number in the format (xxx)xxx-xxxx:");
    number = key.nextLine();        
    for(int i = 0; i < phoneArray.length; i++) {            
        System.out.println("Count of " + i + "'s: " + number.charAt(i));            
    }        
  }      
}

Output:

Enter your phone number in the format (xxx)xxx-xxxx:
(864)728-1638

Count of 0's: (
Count of 1's: 8
Count of 2's: 6
Count of 3's: 4
Count of 4's: )
Count of 5's: 7
Count of 6's: 2
Count of 7's: 8
Count of 8's: -
Count of 9's: 1

I'm not sure what I'm doing wrong.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 4
    Well, you never actually count anything. The `phoneArray` remains untouched. – 001 Oct 15 '18 at 17:05
  • 1
    for starters, you don't seem to be actually counting the digits. What specific problem are you having trouble with? – user3170251 Oct 15 '18 at 17:05
  • You are not counting anything, you are just printing the first 10 elements of the array, start by looping the array, just as you are doing, and you can have another array that stores all the occurence of every number, and based on the current number in your loop you just have to increment the counting array at that postion, in the end you just need to loop the counting array and show the stored numbers – Edu G Oct 15 '18 at 17:06
  • Also important to point out that `charAt()` returns an int, it returns the ASCII code for the character, not a number like 1, 2, 3, .. etc. The ASCII code for `1` is 49 iirc. You're going to have to translate the ASCII codes into the numbers you want, and you'll also have to check for codes that aren't numbers (like `)` and `(` and `-`). – markspace Oct 15 '18 at 17:17

3 Answers3

3

You haven't counted the number of times each digit occurs. Taking the approach you started, you need to iterate over the strings characters and increment the relevant element of the array:

for (int i = 0; i < number.length(); ++i) {
    char digit = number.charAt(i);
    if (digit >= '0' && digit <= '9') {
         phoneArray[i - '0']++;
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

To provide a 'stream-api' solution, here's how you could do it with Collectors.groupingBy:

Map<Character, Long> charsGrouped = IntStream.range(0, str.length())
    .mapToObj(str::charAt)
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

By using this solution you just iterate over your string and group each character and count how many times they occur. From there it's easy to find the most used character.

Christian
  • 22,585
  • 9
  • 80
  • 106
1

You can find the occurences of a digit in the number by eliminating this digit from the number and substracting the length of the remaining string from the length of the original string.
Edit
So if (115)855-4732 is the phone number its length is 13. To count the 1s you remove all the 1s from the number and get (5)855-4732. Now this string has length 11. By substracting 11 from 13 you get 2 occurences of 1s.

for (int i = 0; i < 10; i++) {
    System.out.println("Count of " + i + "'s: " + (number.length() - number.replace("" + i, "").length()));
}
forpas
  • 160,666
  • 10
  • 38
  • 76
  • So basically, it takes into account all numbers in the string and subtracts the number that it is not focused on. For example, in (863)738-3762, if trying to find the amount of 3's present it will do "10-7"? Correct? – Akarian Martin Oct 15 '18 at 17:35