1

I wrote the method 'charCount()' to return String 'chc', but the netbean is forcing me to return null. Does the 'return null' also debarring me use the 'chc' outside this method,or it is well returned. Being new to java I am confused.

           //  static String chc ;

public static String charCount(String [] a){

             String chc ;

   for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length(); j++) {
            char ch = a[i].charAt(j);
            int charcout = a[i].length();
            chc=  Character.toString(ch)+""+Integer.toString(charcout)+" ";
            //  String chc=  ch + "" + charcout + " ";
            return chc;  
            //System.out.print(chc);
        }
    } 
    return null; //NETBEAN IS FORCING ME TO WRITE THIS TO AVOID COMPILE TIME ERROR
}  

I need help to rectify this code to avoid return null.

2
  • 1
    chc is not available outside the scope. You can declare chc before for loop. Commented Jun 7, 2014 at 3:58
  • 1
    If your goal is to return a String describing the frequency of every character in every String in the array, this method's logic will not accomplish that. Once you fix the logic, the requirement to have return null at the end of the method probably will become irrelevant. (Calling return in the middle of the loop means both loops will never execute more than once.) Commented Jun 7, 2014 at 4:04

2 Answers 2

2

It simply wants you to return a "default" value in case your loop never gets executed (aka: if the array has no elements in it). You can either return null or a String (or a subclass of String, but there are none of that).

You cannot use chc there because chc is defined inside the scope of your inner loop. If you want more information about this you should look into "variable scope".

Sign up to request clarification or add additional context in comments.

3 Comments

I am much aware of variable scope, I already I tried both places outside the loop (as edited now) but return null is still wanted by the netbean to compile.
What I have followed from you is that return null is ok here, in case the array is empty 'return null' is active. Being new to programming it is a great lesson from you. I have edited the code, kindly confirm that now it can return String "chc". If not then what change can is needed in the code to got the "chc" returned.
It will always want you to return something: either null or a type that is or inherits from String. Also note that you have to initialize local variables so what you have won't compile. You can no write return null, return chc or return "chc".
0

Java local variables must be initialized before use. The compiler can't be sure the loop will return a value, or even if it will iterate.

But!

Why are you even using a variable... eliminate it!

public static String charCount(String [] a){
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length(); j++) {
            char ch = a[i].charAt(j);
            int charcout = a[i].length();
            return Character.toString(ch)+""+Integer.toString(charcout)+" ";
        }
    } 
    return null;
} 

This code is equivalent to yours, but doesn't use a variable.

As a side note, there doesn't seem much point in having a loop, as it returns on the first iteration of the inner loop. There is probably an error in your logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.