【问题标题】:Comparing String inputs to desired length将字符串输入与所需长度进行比较
【发布时间】:2015-01-09 21:07:24
【问题描述】:

我正在运行这个程序,它从命令行输入 t(Desired Length),no of test
例(N)。但它给出了运行时错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index
    out of range: -1
        at java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:979)
      at java.lang.StringBuilder.insert(StringBuilder.java:300)
        at Test.main(Test.java:28)

我在访问错误索引的地方感到困惑。

import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        String line = br.readLine();
        int N = Integer.parseInt(line);
        String[] profiles = null;
        for (int i = 0; i < N; i++) {
            profiles = br.readLine().trim().split(" ");
        }
        for (int i = 0; i < N; i++) {
            if (Integer.parseInt(profiles[0]) > t && Integer.parseInt(profiles[1]) == t ||
                    Integer.parseInt(profiles[0]) > t || Integer.parseInt(profiles[1]) > t
                    || Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) > t)
            {
                System.out.println("Crop");
            }
            if (Integer.parseInt(profiles[0]) < t && Integer.parseInt(profiles[1]) < t) {
                System.out.println("Upload next");
            }

            if (Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) == t) {
                System.out.println("Accepted");
            }
        }
        System.out.println("Hello World!");
    }
}

【问题讨论】:

  • “我在哪里访问错误的索引”很容易回答:Test.java line 28
  • 你的文件是哪一行?您可以添加评论以找到它吗?
  • 您的代码的目标是什么?检查 N 字符串是否更短、等于或长于 t
  • @Sam 它给了 System.out.println("Upload next")...如果那会在其他行给我一个错误,那么我很容易..调试但它在第 28 行给出-->System.out.println("Upload next").
  • @Giulio 我正在尝试 --> 第一行包含 Length=t。第二行包含 N,照片数量。以下 N 行,每行包含两个空格分隔的整数 Width 和 Height。

标签: java


【解决方案1】:

您的编码不安全,因为您在 profiles 上使用索引 [0][1] 而不检查此数组是否包含任何元素。此外,您循环覆盖profiles 的内容。当br 没有可读取的文本时会发生什么?

您应该始终在访问之前检查值(就像在防御性编程中一样)。

这里可能需要一些Rubber Duck debugging

【讨论】:

  • @Brian..thanks brian 我错过了这一点..我将用 if (line!=null && (line.trim().equals("") || line.trim( ).length()
【解决方案2】:

我不明白你想要做的检查,但是如果你想检查每个字符串是否更长、更短或等于t,你应该使用这个代码:

public class Main
{
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


    public static void main(String args[]) throws IOException {
        int t = Integer.parseInt(br.readLine());
        int N = Integer.parseInt(br.readLine());

        /* MUST INITIALISE BEFORE FILLING IT! */
        String [] profiles = new String[N];
        for (int i = 0; i < N; i++) {
            profiles[i] = br.readLine().trim();
            if (profiles[i].length() < t)
                System.out.println("shorter than t");// do something
            else if (profiles[i].length() == t)
                System.out.println("equals to t");// do something
            else
                System.out.println("longer than t");// do something
        }
    }
}

【讨论】:

    【解决方案3】:

    您正在使用缓冲区读取器读取一些行,使用 System.in 。在关闭此缓冲区之前,您尝试使用 System.out 。这可能是你麻烦的原因吗?我认为要求系统同时写入和读取绝对不是一个好主意。您可以尝试在第二个循环之前放置一个 br.close() 吗?

    【讨论】:

      【解决方案4】:

      这是你代码中的恶魔:

       // Let t=3 and N=5
          String [] profiles=null; // Let the Input Here Be 12, 23, 34, 45, 56
          for (int i = 0; i < N; i++) {
              profiles=br.readLine().trim().split(" ");
          }
          System.out.println(Arrays.toString(profiles));  // [56] Only ? because you are initializing the same rer again and again 
      

      所以当执行到这里时:

      for (int i = 0; i < N; i++) {
          if (Integer.parseInt(profiles[0]) > t && 
              Integer.parseInt(profiles[1]) == t || ... // Throws Index out of bounds exception
      

      编辑 1:这是完整的工作代码:

      package com.stackoverflow;
      
      import java.io.BufferedReader;
      import java.io.InputStreamReader;
      
      public class TestClass {
          public static void main(String args[]) throws Exception {
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              int t = Integer.parseInt(br.readLine());
              System.out.println(t);
              String line = br.readLine();
              int N = Integer.parseInt(line);
              System.out.println(N);
              String[] profiles = new String[N];
              for (int i = 0; i < N; i++) {
                  profiles[i] = br.readLine().trim();
              }
              for (int i = 0; i < N; i++) {
                  System.out.println(Integer.parseInt(profiles[0]) > t && Integer.parseInt(profiles[1]) == t);
                  System.out.println(Integer.parseInt(profiles[0]) > t || Integer.parseInt(profiles[1]) > t);
                  System.out.println(Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) > t);
                  if (Integer.parseInt(profiles[0]) > t && Integer.parseInt(profiles[1]) == t ||
                          Integer.parseInt(profiles[0]) > t || Integer.parseInt(profiles[1]) > t
                          || Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) > t)
                  {
                      System.out.println("Crop");
                  }
      
                  System.out.println(Integer.parseInt(profiles[0]) < t && Integer.parseInt(profiles[1]) < t);
                  if (Integer.parseInt(profiles[0]) < t && Integer.parseInt(profiles[1]) < t) {
                      System.out.println("Upload next");
                  }
      
                  System.out.println(Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) == t);
                  if (Integer.parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) == t) {
                      System.out.println("Accepted");
                  }
              }
              System.out.println("Hello World!");
          }
      }
      

      编辑 2: 在 for 循环中为 if 条件添加测试用例:

      测试用例 1 的值:设 t=3,N=5,五个(即 N)测试用例的值是 12、23、34、45、56

      将“裁剪”打印到控制台。

      测试用例 2 的值: 设 t=30,N=5,五个(即 N)测试用例的值为 12、23、34、45、56

      将“上传下一个”打印到控制台。

      测试用例 3 的值:设 t=10,N=5,五个(即 N)测试用例的值为 10、20、34、45、56

      将“已接受”打印到控制台。

      【讨论】:

      • 是的,这是正确的..但是当我这样做时..它为每个输入提供输出为“作物”..你认为它必须对这种情况做些什么 if (Integer.parseInt( profile[0]) > t && Integer.parseInt(profiles[1]) == t || Integer.parseInt(profiles[0]) > t || Integer.parseInt(profiles[1]) > t || Integer. parseInt(profiles[0]) == t && Integer.parseInt(profiles[1]) > t)
      • @MohitDarmwal 是的,其中一个定义的条件结果是正确的,因为您使用的是 ||运算符,这将被执行
      • 在这个条件下profiles=br.readLine().trim().split(" ");它需要 2 个测试用例值,例如如果我有 2 个测试用例,其值为 180 180 和 456 564 且 t=180 那么它只是比较 456 564 ..为什么要覆盖先前的输入..而不是处理那个???
      • @MohitDarmwal,它也完全取决于你传递给profiles[]数组的值
      • @MohitDarmwal 因为profiles=br.readLine().trim().split(" ");总是创建一个新数组。所以只有最后一个测试用例的值才能使用它
      猜你喜欢
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多