vendredi 23 février 2018

Getting "No line found" when pulling values from an external file

Alright, so I need to grab some strings at random from a file not of a fixed size. So, to do this, I start by counting the lines in the file using a simple while loop. After that, I simply feed that value to a random object and store the ten values I need into the array:

int[] select = new int[10];

I run into problems however with this code.

 String[] value = new String[10];
 for (sel = 0; sel <= 9; sel++){
      int w = select[sel];
      Scanner input = new Scanner(file.txt);      
      for (int q = 0; q <= w; q++){ 
        String bank = input.nextLine();
        input.nextLine();
        value[sel] = bank;
      }

The code works, but it seems that if the value the code is looking for is too high, (tested at about 90+) I get the exception:

java.util.NoSuchElementException: No line found




Aucun commentaire:

Enregistrer un commentaire