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

java arrays string methods

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

String methods in Java || Important string methods

String methods in java || Important string methods

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

Convert String To Array Using Javascript Split Method

In this tutorial, you’ll be going to learn how to convert string to array using javascript split method. The easiest approach to use javascript built-in method String.split(). JavaScript split string method return array of a substring after splitting a string based on the separator you provide. The separator might be a string, special character or […]

Java Array Methods – How to Print an Array in Java

We can not print arrays in Java using a plain System.out.println() method. Instead, these are the following ways we can print an array: Loops: for loop and for-each loop; Arrays.toString() method; Arrays.deepToString() method; Arrays.asList() method; Java Iterator interface; Java Stream API

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.