Help me again

Hey all again,
I really wish my teacher could help me out but he’s kinda useless. You guys are always more helpful.
This is my exercise:
Write a method that counts the occurrences of each digit in a
string using the following header:
public static int count(String s)
The method counts how many times a digit appears in the string. The return value is an array of
ten elements, each of which holds the count for a digit. For example, after executing int counts
= count(“12203AB3”), counts[0] is 1, counts[1] is 1, counts[2] is 2, and counts[3] is 2. Write a
test program that prompts the user to enter a string and displays the number of occurrences of each
digit in the string

This is my code:
//import java util library

import java.util.*;


public class Problem2 {

//main method

public static void main(String[] args){

Scanner scanner = new Scanner(;

String[] myStringArray = new String[10];

System.out.println("Enter a string of " + myStringArray.length + " digits: ");

String s = scanner.nextLine();

myStringArray = s; 


//method header

public static int[] count(String s){ 




it gives me errors and I have no idea how to continue. Please help. I’m having a breakdown over here.

Hello! Could you post some of the errors you are getting?

This one gives me a problem with the line

myStringArray = s;
Type mismatch: cannot convert from String to String

It has given me similar errors with past attempts at making an array out of user input. If that is simply not possible, then I don’t know how to move on in the problem.

What is your logic behind myStringArray = s;? Is your plan to use myStringArray to hold the results, or are you wanting to convert s to an array? If you’re attempting the later, you might find the String split() method useful.

I’m hoping to turn the user input into an array. Given the task of determining the number of occurrences of the numbers entered, that seemed like the best way. Please correct me if I’m wrong, I really don’t know if I’m on the right track.
Oh, what’s the String split () method? That sounds useful.
The fun twist in all of this is that my proffessor didn’t go over these things before assigning this homework hahaha

The String split() method is how you convert a string to an array. For example:

String myString = "Hello, World!";
String[] myStringArray = new String[myString.length()];
myStringArray = myString.split(""); //by passing an empty string as the argument, the string will be split on each character
//if you print myStringArray, it would look like this:
//[H, e, l, l, o, ,,  , W, o, r, l, d, !]
//each character is now an element of the array

You’re not wrong. That’s a good strategy. For more on the split() method try googling ‘java string split method’.

That’s awesome, thank you for your help so far. I’m going to implement that on my next try.
In this case, how would I convert the users input into the array using this method? Would it still be
String myString = s; ?

You just need to call the .split("") method on s like I did on myString in my example. Same syntax using your own variable, s.

Ok everyone, I rethought my approach and I think I might be on a better track with just one issue.
Here is my code:

import java.util.*;

public class Problem2 {

    public static void main(String[] args){

        //create scanner

        Scanner sc = new Scanner(;

        //create array

        String[] array = new String[10];

        //for loop

        for(int i = 0; i<10; i++){

            //Ask for user input

            System.out.print("Enter a string of 10 digits: ");

            array[i] = sc.nextLine();




    public static int[] count(String s){



I think I’ve managed to store the digits into an array with no issue, and it gives me no errors. The problem comes with the next step. I’ll eventually need to write a method that tells me the occurrences of each digit, and for that my professor requires we use the following method header:

public static int count(String s)

which makes me think that I shouldn’t have stored the string in an array in the first place, it should probably just be a normal string. But going about it that way gave me problems and errors, even with your suggestions.
Does anyone know how I could go about this using this new approach? Or will I have to go back to the way it was before?

If you want to convert the string to an array, you should wait, and do it after passing it to the method which requires a string parameter. You could have the user enter a single string, pass it to the count method, and convert it to an array there. If you aren’t allowed to use the .split() method, you could use a similar strategy to what you used above. Use a for loop along with a string method that will allow you to get a single character from the string (of course in doing that, you could just compare that character and not bother with putting it in an array first). You might check out the charAt method.