Link to home
Start Free TrialLog in
Avatar of pdidow
pdidow

asked on

Help with function

Here is the function:

private static int CountHits(int userguess[], int adigit[], int digits,String args[]) throws Exception
{
      int Hits=0;
      int Misses=0;
      int x=0;

      for (x=0;x<digits;x++)
      {
            if (userguess[x]==adigit[x])
            {
                  Hits++;
            }
      }
//diagnostic
      String strChar = String.valueOf(args[0]);
                                                                        
if (strChar.equals("testing"))
      {
            
            x=0;
            System.out.print("DIAGNOSTIC:");
            for (x=0;x<digits;x++)
            {
                  System.out.print(adigit[x] + " ");  
            }

            System.out.print("\n");
            System.out.print("HIT RESULT:");
            for (x=0;x<digits;x++)
            {
                  if  (userguess[x]==adigit[x])
                  {
                        System.out.print("H ");  
                  }
                  else
                  {
                        System.out.print(". ");
                  }
            
            }
            System.out.print("\n");
      }
return Hits;

}

When I pass in this:
java Lab2 "testing"
everything works fine.

when i dont pass it in, I get an ArrayIndexOutOfBoundsException


how do i correct this?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

can u post your main
>      String strChar = String.valueOf(args[0]);

If thats the args from main then thatline will cause that error
because args will be empty
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial