I'm having an issue with a nest while loop that is causing the following:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Chatbot.main(Chatbot.java:25)
I'm not really sure what is causing the ArrayIndexOutOfBoundsException.
Part of the requirements of this, is that I'm not allow to use any other form of loop other than a while loop. Also can't use any continue,break etc.
Any insight would be highly appreciated.
Nik
Code Below:
import javax.swing.JOptionPane;
public class Chatbot {
public static void main(String[] args) {
String[] topics;
int numTopics = Integer.parseInt(JOptionPane.showInputDialog("How many topics would you like to talk about?"));
while (numTopics < 1) {
numTopics = Integer.parseInt(JOptionPane.showInputDialog("Error! Please enter a positive number of topics!"));
}
int i = 0;
topics = new String[numTopics];
while (i < topics.length) {
topics[i] = JOptionPane.showInputDialog("Please enter topic " + i);
while (topics[i].contains("?")) {
topics[i] = JOptionPane.showInputDialog("I'll ask the questions here, Please enter topic " + i);
}
i = i + 1;
}
String dialog1 = JOptionPane.showInputDialog("Tell me more about " + topics[numTopics]);
}
}