Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.*; import java.util.*; public class TestCreator1 { public static

ID: 3762923 • Letter: I

Question

import java.io.*;
import java.util.*;

public class TestCreator1
{
public static void main (String[] args) throws IOException
{
String test, AnswerChoices, Key, Questions, Response, Test;
File inputFile;
int Lines;
int numQuestions = 0;
int numChoices = 0;
  
  
Scanner kb = new Scanner(System.in);
System.out.println("Please enter a test to open:");
test = kb.nextLine();
inputFile = new File(test);
  
  
if(!inputFile.exists())
{
System.out.print("The test " + """+inputFile +""" + " does not exist. Please enter another file name: ");
test = kb.nextLine();
inputFile = new File(test);
}
  
  
Scanner file = new Scanner(System.in);
file = new Scanner(inputFile);
while(file.hasNext())
{
numChoices = file.nextInt();
numQuestions = file.nextInt();
String[] keyArray = new String[numQuestions];
String[] questionsArray = new String[numQuestions];
String[] optionsArray= new String[numChoices];
  
for (int d=0; d<numChoices; d++)
{
optionsArray[d] = file.next();
}
for (int q=0; numQuestions>q; q++)
{
keyArray[q] = file.next();
}
for (int i=0; i<numQuestions; i++)
{
questionsArray[i] = file.nextLine();
}

for (int o=1; o<questionsArray.length+1; o++)
{
System.out.println(questionsArray[o]);
  
for (int t=0; t<optionsArray.length; t++)
{
System.out.println(optionsArray[t]);
}
  
Scanner answerBox = new Scanner(System.in);
Response = answerBox.nextLine();
  
}
}
  
  
}
}

______________________________________________________

Keep getting the following error:

"java.lang.ArrayIndexOutOfBoundsException: 4"

help?

Explanation / Answer

import java.io.*;
import java.util.*;
public class TestCreator1
{
public static void main (String[] args) throws IOException
{
String test, AnswerChoices, Key, Questions, Response, Test;
File inputFile;
int Lines;
int numQuestions = 0;
int numChoices = 0;
  
  
Scanner kb = new Scanner(System.in);
System.out.println("Please enter a test to open:");
test = kb.nextLine();
inputFile = new File(test);
  
  
if(!inputFile.exists())
{
System.out.print("The test " + """+inputFile +""" + " does not exist. Please enter another file name: ");
test = kb.nextLine();
inputFile = new File(test);
}
  
  
Scanner file = new Scanner(System.in);
file = new Scanner(inputFile);
while(file.hasNext())
{
numChoices = file.nextInt();
numQuestions = file.nextInt();
String[] keyArray = new String[numQuestions];
String[] questionsArray = new String[numQuestions];
String[] optionsArray= new String[numChoices];
  
for (int d=0; d<numChoices; d++)
{
optionsArray[d] = file.next();
}
for (int q=0; numQuestions>q; q++)
{
keyArray[q] = file.next();
}
for (int i=0; i<numQuestions; i++)
{
questionsArray[i] = file.nextLine();
}
for (int o=0; o<questionsArray.length; o++)
{
System.out.println(questionsArray[o]);
  
for (int t=0; t<optionsArray.length; t++)
{
System.out.println(optionsArray[t]);
}
  
Scanner answerBox = new Scanner(System.in);
Response = answerBox.nextLine();
  
}
}
  
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote