-1
public class Reviewmaker {
    public static void main (String args[]) {
        System.out.println("Review Maker");     
        System.out.println("----------------------------------------------------");
        System.out.println("By: Sami Besellam");

        String Question, Answer;
        System.out.println("How many questions do you Want?");
        int f = Expo.enterInt();
        int e = 1;
        String[] Q = new String [e]; 
        String[] A = new String [e]; 

        for (int k = 1; k <= f; k++) {
            System.out.println("Enter question  " +k);
            Q[k] = Expo.enterString(); 

            System.out.println("Enter Answer " +k);
            A[k] = Expo.enterString();
        }

        for (int k = 1; k <= f; k++) {
            System.out.println("Question #" + k);
            System.out.print(Q[k]);
            System.out.println("Question #" +k);
            System.out.print(A[k]);
        }
    }
}


// Q = E + k

It gives me this error message when I type in my first question "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1" at Reviewmaker.main(Reviewmaker.java:18)

By the way I am using Jgrasp Expo.io if any one is familiar it would be greatly appreciated

Atomzwieback
  • 585
  • 6
  • 18

3 Answers3

1

change String[] Q = new String [e]; to String[] Q = new String [k]; same with your A array. your variable e is unnecessary.

Justin
  • 1,356
  • 2
  • 9
  • 16
0

i guess what you need is

String[] Q = new String [f]; 
String[] A = new String [f]; 

and also run your for loop till 1 less than f not till f

for (int k = 1; k < f; k++)
nishant
  • 2,526
  • 1
  • 13
  • 19
0

You are storing number of questions in variable f, but you are defining size of your question Q and answer A array with variable e.

Now if I entered number of question to be 5, that means f=5. But you array size is 1, so when for loop runs it will run from 0 to 4 and when second element of your arrays are accessed it will give exceptions because of size.

Also run your loop from 0 index not from 1 to

Nishesh Pratap Singh
  • 2,131
  • 2
  • 13
  • 20