代码之家  ›  专栏  ›  技术社区  ›  Walker

如何修复StringOutOfBoundsException?[重复]

  •  -1
  • Walker  · 技术社区  · 2 年前

    我正在为一个学校项目编写代码,而我在第50行中不断得到StringOutOfBoundsException。

    这是我的代码:

    import java.io.*; 
    import java.util.*;
    
    class Main {
      public static void main(String[] args) {
        String end = "e";
        int [][] seat = new int [12][20];
        for (int rows = 0; rows<seat.length;rows++){
            for (int cols = 0; cols<seat[0].length; cols++){
              seat[rows][cols] = 0; 
            }
          }
        while (end!="q"){
          System.out.println();
          System.out.print("\t");
          for (int cols = 0; cols<seat[0].length;cols++){
            System.out.print(cols+1 + "\t");
            }
          System.out.println();
          for (int rows = 0; rows<seat.length;rows++){
            System.out.print(rows+1 + "\t");
            for (int cols = 0; cols<seat[0].length; cols++){
              System.out.print("[" + seat[rows][cols] + "]" + "\t");
            }
            System.out.print( rows+1 );
            System.out.println();
          }
          System.out.println();
          String h = "Cinema Screen";
          System.out.println(String.format("%45s", h));
          
          try{
            System.out.println();
            System.out.print("Which column is your desired seat in? ");
            Scanner reader = new Scanner(System.in);
            int column = reader.nextInt();
            System.out.print("Which row is your desired seat in? ");
            int row = reader.nextInt();
            System.out.println();
            if (seat[row-1][column-1] == 1){
              System.out.print("This seat is already taken. ");
            }
            else if (seat[row-1][column-1] != 1){
              seat[row-1][column-1] = 1;
              System.out.print("\t");
            }
            System.out.println();
            System.out.print("Would you like to continue? ");
            String cont = reader.nextLine();
            char contin = cont.charAt(0);
            if (contin == 'n' || contin == 'N'){
              end = "q";
            }
          } 
          catch (InputMismatchException e){
            System.out.println();
            System.out.print("The input has to be a valid integer.");
          }
          catch (ArrayIndexOutOfBoundsException e){
            System.out.println();
            System.out.print("Please choose an integer representing an existing seat.");
          }
        }
        
        
      }
    }
    

    我一直收到的例外是 char contin = cont.charAt(0); ,它一直说索引0超出范围。

    我不知道我做错了什么,我不想用try-catch语句来修复它。有人能帮我吗?

    2 回复  |  直到 2 年前
        1
  •  0
  •   Tyler VanHaren    2 年前

    当您调用nextInt()时(或任何形式),扫描器标记当前行并返回下一个标记,但被解析为您调用的类型,但它不会刷新整行。如果在包含“4 5”的单行上调用两次nextLine(),它会同时返回这两个值——这里的问题是,当在nextLine()之后调用nextLine()时,实际上是在缓冲区中获得当前行的其余部分(在您的情况下,似乎只是一个行尾字符)。

    https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Scanner.html 对于扫描仪上的完整文档。

        2
  •  0
  •   tntzx    2 年前

    所以基本上问题是当你在输入行号后按enter键时。在该场景中,会跳过nextline()添加读取器。nextLine();之前 String cont=读卡器。nextLine();

     import java.io.*; 
     import java.util.*;
    
     class Main {
     public static void main(String[] args) {
     String end = "e";
     int [][] seat = new int [12][20];
     for (int rows = 0; rows<seat.length;rows++){
        for (int cols = 0; cols<seat[0].length; cols++){
          seat[rows][cols] = 0; 
        }
      }
     while (end!="q"){
      System.out.println();
      System.out.print("\t");
      for (int cols = 0; cols<seat[0].length;cols++){
        System.out.print(cols+1 + "\t");
        }
      System.out.println();
      for (int rows = 0; rows<seat.length;rows++){
        System.out.print(rows+1 + "\t");
        for (int cols = 0; cols<seat[0].length; cols++){
          System.out.print("[" + seat[rows][cols] + "]" + "\t");
        }
        System.out.print( rows+1 );
        System.out.println();
      }
      System.out.println();
      String h = "Cinema Screen";
      System.out.println(String.format("%45s", h));
      
      try{
        System.out.println();
        System.out.print("Which column is your desired seat in? ");
        Scanner reader = new Scanner(System.in);
        int column = reader.nextInt();
        System.out.print("Which row is your desired seat in? ");
        int row = reader.nextInt();
        System.out.println();
        if (seat[row-1][column-1] == 1){
          System.out.print("This seat is already taken. ");
        }
        else if (seat[row-1][column-1] != 1){
          seat[row-1][column-1] = 1;
          System.out.print("\t");
        }
        
        
        System.out.println();
        System.out.print("Would you like to continue? ");
        reader.nextLine();  // newline 
        String cont = reader.nextLine();
        char contin = cont.charAt(0);
        if (contin == 'n' || contin == 'N'){
          end = "q";
        }
        
        
        
      } 
      catch (InputMismatchException e){
        System.out.println();
        System.out.print("The input has to be a valid integer.");
      }
      catch (ArrayIndexOutOfBoundsException e){
        System.out.println();
        System.out.print("Please choose an integer representing an existing seat.");
      }
    }
    
    
    }
    }