having java.lang.ArrayIndexOutOfBoundsException error come up, as well as j not seeming like a variable in print statement, when is declared in loop

Viewed 18
import java.util.Scanner;
public class DriversLicense {

    public static void main(String[]args){
        
        Scanner input = new Scanner(System.in);
        
        int count = 0;
        int wrong = 0;
        String pass;
        int j;
        
        String[] arr = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"};
        String[] answers = new String [arr.length];
        int[] w = new int[arr.length];
        
        for(int i = 0; i < arr.length; i++) {
            System.out.println("Enter your answer:");
            answers[i] = input.next();
            if(answers[i].equals(arr[i])) {
                count++;
            }
            else {
                wrong++; 
                w[i] = i;
            }
        }
        int[] y = TestDriversLicense.numWrong(arr, answers, w, wrong);
        if((TestDriversLicense.Pass(count))){
            pass = "Pass!";
        }
        else {
            pass = "Fail!";
        }
        System.out.println(pass);
        System.out.println("You got "+ TestDriversLicense.Total(count)+ " correct!");
        System.out.println("You got "+ TestDriversLicense.Inc(wrong)+ " wrong!");
        if(wrong == 0) {
        }
        else {
            for(j = 0; j < y.length; j++);
            System.out.println("Your wrong answers were number(s) #"+ y[j]);
//attempting to print out the numbers for the wrong answers
        }
        
    }
    
}

attempting to print out the numbers of the questions answered incorrectly

public class TestDriversLicense {

    public static boolean Pass(int n) {
        int pass = n;
        if(pass >= 15) {
        return true;
        }
        return false;
    }
    public static int Total(int correct) {
        int c = correct;
        return c;
    }
    public static int Inc(int n) {
        int inc = n;
        return inc;
    }
    public static int[] numWrong(String[] a, String[] ans, int[] b, int t) {
        int[] x = new int[t];
        for(int i = 0; i < a.length; i++) {
        }
        x =b;
        return x;
    }
    
}
0 Answers
Related