I am trying to write a program in java that asks the user to input a string(telephone number). Using an array of size 10 I need to count each character and increment fro each time the number is used. Here is what I have so far: public static void main(String[] args) { Scanner userInput = new Scanner(System.in); String phoneNumber = userInput.next(); int[] numberArray = new int[10]; int[] countNumbers = new int[10]; for(int phone : phoneNumber){ ++numberArray[phone];
So you want the person to put in each number separately? You might want to use a for loop to do this.
for loop. Here is what I have but it is out of bounds. Can you help me see what is wrong thanks. I need to store the users string into an array. the problem is at char[] arrayNumber, it will let me put numbers but not the userInput variable package chap_07_codeexamples; import java.util.Scanner; public class CharacterFrequency1 { public static void main(String[] args) { Scanner userInput = new Scanner(System.in); System.out.print("Give me a word of 10 characters. " + "\nI will tell you if it contains any numbers, and how many: "); String phoneNumber = userInput.nextLine(); char number1; number1 = phoneNumber.charAt(0); char[] arrayNumber = new char[number1]; int[]occurence = new int[10]; if(phoneNumber.length() > 10){ System.out.println("Wrong length, you have entered invalid word."); System.exit(0); }else{ System.out.println("The count for each digit is:"); for ( int number : arrayNumber) { ++occurence [number]; } int numberCount = 0; for (int occur : occurence) { System.out.print("\n\tCount of " + numberCount + " is " + occur); numberCount++; } } }}
Java is NOT my strongest language. Isn't char a primitive data type? Assuming it is, you can't create one with a new statement. new is for class instantiation.
Yes, but he never attempts to do this @rsmith6559 since he's really making an Array of chars.
I'm going to go ahead and remove all the code that's not important such as what happens if the phone number is too long. Actually while we're talking about it, you might want to consider updating it to: ``` if (phoneNumber.length() != 10 ) ``` So that it will exclude anything that's not 10 digits. But moving on to the real stuff...
``` // Stuff String phoneNumber = userInput.nextLine(); char[] arrayNumber = phoneNumber.toCharArray(); int[] occurrence = new int[10]; //More stuff ``` That should essentially solve your problem, since as you currently have it you're only sort of doing a weird thing where you only have one number even trying to be put into your array. The `toCharArray()` method automatically turns your String into an Array of chars. But you're not really out of the woods yet, it looks like you still have some more problems to work out. Good luck and show me what progress you make on it. Don't forget, `System.out.println()` is a great way to just run your program and test what values something has at a certain point as a method of debugging.
Join our real-time social learning platform and learn together with your friends!