简体   繁体   中英

Java: error when trying to break while loop

I keep getting this error when the program is supposed to reach break:

"Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 at java.util.LinkedList.checkElementIndex(LinkedList.java:555) at java.util.LinkedList.get(LinkedList.java:476) at Glosor.main(Glosor.java:65)"

This is the code:

  import java.util.Arrays;
  import java.util.LinkedList;
  import java.util.List;
  import java.io.*;

  public class Glosor {
  public static void main(String[] args) throws IOException {

  List<String> gloslista1 = new LinkedList<String>(Arrays.asList());
  List<String> gloslista2 = new LinkedList<String>(Arrays.asList());

  String inputStr1 = JOptionPane.showInputDialog(null,
                                               "**********************************" + "\n\n" + 
                                               "1. Skapa glosövning" + "\n\n" + 
                                               "2. Starta glosövning" + "\n\n" +
                                               "3. Avsluta" + "\n\n" + 
                                               "**********************************");

  int input1 = Integer.parseInt(inputStr1);




     switch (input1) {

        case 1:

        String övningsnamn = JOptionPane.showInputDialog(null, "Vad heter övningen?");
        String språk1 = JOptionPane.showInputDialog(null, "Språk 1?");      
        String språk2 = JOptionPane.showInputDialog(null, "Språk 2?");

           while (true) {

           String glosa1 = JOptionPane.showInputDialog(null, "Skriv in glosa på " + språk1 + "\n\n" + 
                                                             "När du är klar skriv klar i rutan");               
              if(glosa1.equals("klar")) {
              break;
              //this is where i get the error message
              }
              else {          
              String glosa2 = JOptionPane.showInputDialog(null, "Skriv in glosa på " + språk2); 


              gloslista1.add(glosa1);
              gloslista2.add(glosa2);
              }                      

           }
              String filnamn1 = "svenskaord";
              String filnamn2 = "franskaord";

              PrintWriter utström1 = new PrintWriter
                                     (new BufferedWriter
                                     (new FileWriter(filnamn1)));
              //Skapar en text fil för glosorna på svenska

              PrintWriter utström2 = new PrintWriter
                                     (new BufferedWriter
                                     (new FileWriter(filnamn2)));
              //Skapar en text fil för glosorna på franska   

                 for(int i = 0; i<=gloslista1.size(); i++) {
                 utström1.println(gloslista1.get(i));
                 utström2.println(gloslista2.get(i));
                 //Skriver in glosor i text filerna

                 }

Loop should be

for(int i = 0; i<=gloslista1.size()-1; i++) 

means it should end to gloslista1.size()-1 .The stacktrace is saying that you have 2 element in list so you have 2 indexes in the list ie 0 and 1 but you are trying to access index 2 which is not present in the list giving you the exception.

This for loop is going out of bounds.

for(int i = 0; i<=gloslista1.size(); i++) {
    utström1.println(gloslista1.get(i));
    utström2.println(gloslista2.get(i));
    //Skriver in glosor i text filerna

}

Doing gloslista1.get(i) when i is equal to gloslista1.size() is out of bounds since the list indices go from 0 to gloslista1.size()-1 . The for loop needs to be as singhakash suggested, or more idiomatically like so:

for(int i = 0; i<gloslista1.size(); i++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM