How to fix “exception in thread main java.lang.stringindexoutofboundsexception string index out of range” errors

I'm trying to recreate the game of mastermind except a simplified version. I've been stuck on this one error and I don't know how to fix it.

String combo = "";
int plus = 0;
String in = "";
int right = 0;
int numbers = 6;
int length = 4;
int tries = 10;
int[] guessNums = new int[numbers];
int[] comboNums = new int[numbers];

rules();
combo = guesses();

for(int i = 0; i < length; i++)

{
if(in.charAt(i) == combo.charAt(i))
{
right++;
}
guessNums[in.charAt(i)-49]++;
comboNums[combo.charAt(i)-49]++;
}

for(int i = 0; i < numbers; i++)
{
while(comboNums[i] > 0 && guessNums[i] > 0)
{
plus++;
comboNums[i]–;
guessNums[i]–;
}
}
String reset = “\u001B[0m”;
System.out.print(“\t”);
String a = “” + right;
printRed(a);
System.out.println(" " + reset + “” + plus);
System.out.println(“\n”);

tries–;
return(right == length);


it says the error is in line if(in.charAt(i) == combo.charAt(i)).

#java #arrays #string #methods

4 Likes42.25 GEEK