【问题标题】:Error Handling (Java)错误处理 (Java)
【发布时间】:2017-02-25 01:57:30
【问题描述】:

相当简单的问题,但我基本上得到了调试代码,我已经修复了除一个以外的所有错误。在尝试使程序更友好并包含错误处理时,我发现即使满足条件(即用户搜索的数组中的数字实际上存在于数组中)也会抛出错误消息。不是在寻找直接的答案,只是一个提示。我尝试过使用 if/else 的组合以及在花括号周围移动。

    Scanner input = new Scanner(System.in);
    System.out.println("Enter an integer to find: ");

    try {
        int number = input.nextInt();
        int index = Arrays.binarySearch(array, number);

        for (int i = 0; i < array.length; i++) {
               if ( array[i] == number )
                   System.out.println("Found " + number + " at index " + index++);
       }
            System.out.printf("Your number was not found within the array.");
    }
    catch (InputMismatchException e){
            System.out.printf("Sorry, but it looks like you entered something other than an integer. Please try again.");   
    }
}

控制台输出示例:

Enter an integer to find: -9

在索引 0 处找到 -9
在数组中找不到您的号码。

【问题讨论】:

  • array 中的第一个条目不是正确的数字时,您认为会发生什么?
  • 只需在 else 子句中打印错误消息。不要抛出异常。
  • @tkausl 嗯,所以它只检查数组中的第一个数字吗?
  • @Jecoms 这有所帮助,但该消息重复了很多次。
  • 循环的目的是什么?如果索引存在,则您已经使用二进制搜索数组函数获取索引。如果index 为-1,则找不到您的号码。

标签: java arrays exception error-handling


【解决方案1】:

Array.binarySearch 如果找到值将返回索引,否则返回-1。

如果index == -1,可以打印“未找到消息”,根本不进入循环。

否则,如果index &gt; 0,那么你可以进入循环并遍历数组以找到每个值匹配的索引。

如果您想要一条消息用于多个匹配项,这是必需的,因为 binarySearch 只会返回找到该值的第一个索引。

顺便说一句,binarySearch 要求首先对数组进行排序,否则结果将是不确定的。我不知道这是否是一个问题,因为数组是在示例之外声明的。

Scanner input = new Scanner(System.in);
System.out.println("Enter an integer to find: ");

try {
    int number = input.nextInt();
    int index = Arrays.binarySearch(array, number);

    if (index > 0) {
        for (int i = 0; i < array.length; i++) {
           if ( array[i] == number ) {
               System.out.println("Found " + number + " at index " + i);
           }
        }
    } else {
        System.out.printf("Sorry, your number wasn't found.");   
    }
}
catch (InputMismatchException e) {
    System.out.printf("Sorry, but it looks like you entered something other than an integer. Please try again.");  
}

【讨论】:

  • 是的,除了数组表示 index[0 处的值
  • 除了数组表示未找到 index[0] 处的值之外,是的,它应该是这样工作的。
  • @Walby。啊。因此,如果数字出现多次,您需要多条消息。
  • @Walby 消息中的索引引用了二进制搜索索引。它实际上应该在循环中打印出i 的当前值。
【解决方案2】:

在您的代码中:
“在数组中找不到您的号码。” 将始终打印,除非 catch 块捕获到异常

关于二分搜索还有一件有趣的事情是:
如果数组中不存在元素,则返回可能位置的负数如果搜索到的元素“本来”存在。

例子:在这个程序中,如果你搜索9,它会返回-3
如果你搜索 65,它会返回 -6

这将正常工作:

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class debug {
public static void main(String...args){

     Scanner input = new Scanner(System.in);
        System.out.println("Enter an integer to find: ");
        int i, array[]={-9,8,14,56,64};
        try {
            int number = input.nextInt();
            int index = Arrays.binarySearch(array, number);

            if(index>=0)
            System.out.println("Found " + number + " using binary search at index " + index);
            else
                System.out.println("Not Found !! index returned by binary search is : "+index);

            boolean flag=false;
            for (i = 0; i < array.length; i++) {
                   if ( array[i] == number ){
                       flag=true;
                       break;
                   }
           }
            if(flag)
                System.out.println("Found " + number + " using for loop at index " + i);
            else
          System.out.printf("Your number was not found within the array.");
        }
        catch (InputMismatchException e){
                System.out.printf("Sorry, but it looks like you entered something other than an integer. Please try again.");   
        }
    }

}


【讨论】:

    【解决方案3】:

    循环内的条件错误。如果数组的第一个元素不是您要查找的值,它会立即终止。错误处理必须在循环之后。试试这个:

     boolean found = false;
        for (int i = 0; i < array.length; i++) {
            if ( array[i] == number ){
                System.out.println("Found " + number + " at index " + index++);
                found = true;}
        }
        if(!found)
            throw new IllegalArgumentException("Your number was not found within the array.");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 2013-08-16
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多