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

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.

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)).

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to convert an Array to String in Java?

In this post, you'll learn the various methods to convert an Array to String in Java

Java String – String Functions In Java With Examples

In this post, we will be discussing about a new concept, Java String. String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create string object.

A Complete Java Array Tutorial

In the article, we learn about Array, features of Array, Shortcomings of Array, Types of Arrays, Array Methods and how to use the Array Class and methods using Java.