0

I am taking in user input, where input is the number of rotations I have to do with my finished array. I have to have 20 lines of user input. Each each line containing 20 characters of 1's and 0's and I am putting each number into a slot in a two-dimensional array. If I hardcoded the first input it does exactly what I require but for some reason when I try to get this value from the user it gives the error: "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source)" This doesn't happen when I hardcode the first input so I don't know what the issue is when I try get the value from the user.


import java.util.*;
public class try6 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        
        //this is the number of states the game should run for
        int myAr [][] = new int [20][20];
        int i = 0;
        String s1 = new String();
        
        int input = sc.nextInt();
        
        for(int j = 0; j < 20; j ++)
        {
            fill(myAr, sc.nextLine(), i);
            i ++;
            
        }

    }

    public static void fill(int [][] myAr, String s1, int row)
    {
        for(int i =0; i < 20; i ++)
        {
            myAr[row][i]= Character.getNumericValue(s1.charAt(i));
        }
    }
}
Mady Daby
  • 1,237
  • 4
  • 18
user101
  • 1
  • 4

0 Answers0