2

Im building my first program. The idea is to read a list of questions from a file and then allow the user to select a number of random questions to get asked. For instance the program will have 30 questions. However the user may only want to practice 5 questions at a time. I have reached my first real hang up.

Im writing a method that will allow the user to input how many questions they want asked. The method will output an in array of x random numbers that do not exceed the amount of questions. I then plan to use that array to call the actual questions from another method that I have already successfully built.

Here is the code for the method

public int [] ranQuest(int numQs, int totalQs) {

    int [] numberQs = new int [numQs];
    int maxNum = totalQs;

    ArrayList<Integer> allQs = new ArrayList(); 

    for (int x = 0; x < maxNum; x++) {
        allQs.add(x);
    }

    Collections.shuffle(allQs);

    for(int x = 0; x < numberQs.length; x++) {    
       numberQs[x] = allQs.get(x);
    }

    return numberQs;
}

I have commented out my troubleshooting print lines. Here is my test code:

public static void main(String[] args) {

    GameLoad gl = new GameLoad();
    Scanner scan = new Scanner(System.in);
    String userAns = null;
    Random randInt = new Random();
    QuestGen questGen = new QuestGen();

    int [] quests = questGen.ranQuest(4, 10);

    for (int quest : quests) {
        System.out.println(quests[quest]);
    }
}

When I run the code for inputs of 3, 3 or 4, 4 it sudo works. I do not get a outofbounds exception error but the numbers are not in the order I expect. When I run the code for 3, 10 or 5, 30...I get an outofbounds exception error that dumfounds me because I think my for loop is good.

I have done a lot of searching and it seems like I may have a problem trying to convert Integers to ints, or maybe my for loop to populate the int array is not right. Is there a way or better way then the path Im on to take an array of X ints populated in order mix those up, and then populate a new smaller array?

Thanks in advance Jojo

user3138997
  • 230
  • 1
  • 10
jojonac
  • 39
  • 1
  • 1
  • 6
  • 1
    A tip: never declare a list of type `ArrayList`, use `List` instead, which is the class that `ArrayList` implements. This will allow you to easily switch between different implementations (another popular `List` implementation is `LinkedList`). So just go `List myList = new ArrayList();` – dabadaba Sep 20 '16 at 09:22
  • Thank you for the tip. I had seen this done in other places and was not sure what was going on. Your tip cleared it up. – jojonac Sep 20 '16 at 16:55
  • You should check foreach and for difference to have less problems in future Foreach : http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work For : https://www.quora.com/How-does-the-for-loop-work-in-Java Hope it will help you resolve your problem ! – Joseph Gremaud Sep 20 '16 at 09:34

1 Answers1

2
for (int quest : quests) {
    System.out.println(quests[quest]);
}

change it to

for (int quest : quests) {
    System.out.println(quest);
}

Right now if your array contains for example the elements 10, 11 and 12 you are asking for the values of quests[10], quests[11] and quests[12] which are obviously not defined since the array has only three elements.

Turamarth
  • 2,282
  • 4
  • 25
  • 31
  • Man super frustrating. The whole time I was searching for an error in my method and it turns out my test was what was killing me. Thanks for the quick fix. – jojonac Sep 20 '16 at 16:56