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("DIAGNOST IC:");
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 ArrayIndexOutOfBoundsExcep tion
how do i correct this?
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("DIAGNOST
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 ArrayIndexOutOfBoundsExcep
how do i correct this?
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
If thats the args from main then thatline will cause that error
because args will be empty
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.