The purpose of this assignment was to get a phone number and print out how many digits of each were in the phone number.

        Ex:
        phone number = 555-3311

        1 = 2
        2 = 0
        3 = 2


etc.

Instead of getting how many times the number shows up I just get a square and I'm really confused. I'm sure its some simple error but please don't outright tell me. I'd really appreciate it, if I could learn this on my own but I need a little push in the right direction of where to look.



        import java.util.*;

        public class CharacterFrequency {

            public static Scanner kbd = new Scanner(System.in);

            public static void main(String[] args) {

                String telephone, output = "";
                int index = 0;
                char [] numbers = new char[10];

                System.out.print("Please enter a 10-Digit telephone number: ");
                telephone = kbd.nextLine();

                telephone = telephone.replaceAll("[^0-9]","");

                System.out.println("\nThe number of times each digit appears:");

                for(int count = 0; count < telephone.length(); count++)
                {
                    numbers[telephone.charAt(count) - '0']++;
                }

                for(int count = 0; count < numbers.length; count++)
                {
                    if(numbers[count] == 0)
                    {
                        output += (char)('0' + count) + " - 0\n";
                    }

                    if(numbers[count] != 0)
                    {
                        output += (char)('0' + count) + " - " + numbers[count]+"\n";  <--- I think the error is on this line
                    }
                }
                System.out.println(output); 
            }

        }

Wouldn't that just loop back and print over and over?

@X, with a little rework, line 53 can vanish. That is, just System.out.format instead of lines 45 and 50.

That tosses out the need for the output array.

But let's say you don't want to use System.out.format. Look at Integer.toString

Whats the integer.toString? Is that practically the opposite of Integer.parseInt?

Have you considered using an int array of size 10 and initializing each element to 0? Index 0 would keep track of the number of times the number 0 occurs. Index 1 would keep track of the number of times the number 1 occurs...etc.

Break what you are trying to do into smaller pieces. Try searching for the following:

  • java loop through string
  • convert character to int

Yeah, that was actually my first way of thinking but I was having trouble getting it to work properlly so I started over and tried this way instead and so far everything is working but it just prints out squares instead.

Line 50 where you write output += (char)('0' + count) is as if you are thinking C code. I see Java in the tag and I'd use the Java's own Integer.toString(count) instead. Try it Sam I am.

Holy heck! I finally get what you are saying now and I actually just found out why my code wasn't working. Which was would honestly be more efficient, my professor really stresses that. I think you way is more efficient than what I was doing. Thank you soooo much for your help!

I actually added in some print statements and got rid of the output variable. For some reason I just had it in my mind that it was going to print the same thing over and over. I also got rid of the if statement inside the loop, I'm not really sure why I had that in there in the first place.

commented: Skill points added. +6
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.