2016-02-26 93 views
-2

我的代码应做到以下这部分:if语句和的try-catch不会阻止错误

  1. 检查输入是INT型(的try-catch)
  2. 如果输入一个INT,检查它是否在列表之间:

代码

public static void selection(List<Performer> listperformer) 
{ 
    int i=0; 
    List<Performer> idlist = new ArrayList<Performer>(); 
    Scanner sclocal = new Scanner(System.in); 

    if(listperformer.isEmpty() == true) 
    { 
    System.out.println("- empty -"); 
    return; 
    } 
    else 
    { 
    System.out.println("Enter IDs:"); 
    int id; 
    id=sclocal.nextInt(); 

    while(sclocal.hasNext()) 
    { 

    try{ 
    id=sclocal.nextInt(); 
    }catch(InputMismatchException exception) 
    { 
    System.out.println("Invalid input!"); 
    } 

    if(listperformer.size() <= id || id < 0) 
    { 
    System.out.println("Invalid input! 294"); 
    return; 
    } 
    else 
    { 
    idlist.add(listperformer.get(id)); 
    } 
    id=sclocal.nextInt(); 
    } 
    } 

不起作用。问题: 1.

  1. ,如果我把一个错误的ID,他问我把在另一并在此之后 抛出一个异常
  2. ,如果我把一个字符串抛出一个“InputMismatchException时”

我们假设,我们列表中只有三个条目。 结果:

Input: 5 
Output: 

Input: 4 
Output: Invalid input! 294 

Input: asdf 
Output: Exception in thread "main" java.util.InputMismatchException... 
+0

你观察到的“有问题”的用例的工作原理完全一样,意,或能更好地说 - 因为它是编码和预期。 – Shark

+0

为了更好地理解你的代码在做什么,使用几乎可以肯定内置到IDE(或独立调试器)中的调试器,并逐步浏览代码。使用调试器是学习编程的重要部分,而非可选部分。 –

回答

1

我改编了几行,在代码中添加一些注释,并且也改变了你的System.out陈述,以反映什么是真正发生的事情。在你的catch块中,“sclocal.nextLine()”将消耗导致第一个异常的无效输入,以便控制可以前进到下一次迭代。

作为一般指导原则,使用“camelCase”作为变量名是个好主意。

public static void selection(List<Performer> listperformer) { 
    int i = 0; 
    List<Performer> idlist = new ArrayList<Performer>(); 
    Scanner sclocal = new Scanner(System.in); 

    if (listperformer.isEmpty() == true) { 
     System.out.println("- empty -"); 
     return; 
    } else { 
     int id;//This is being used as an offset, so I recommend you rename it to "offset" 
     System.out.println("Enter ID:"); 

     while (sclocal.hasNext()) { 
      try { 
       id = sclocal.nextInt(); 
       if (listperformer.size() <= id || id < 0) { 
        System.out.println("Invalid input! You requested the element at offset [" + id + "], but the max offset available is [" + (listperformer.size()-1) + "]. Exiting."); 
        return; 
       } else { 
        System.out.println("Input is valid. We have added the offset identifier [" + id + "] to listperformer."); 
        idlist.add(listperformer.get(id)); 
       } 
      } catch (InputMismatchException exception) { 
       System.out.println("Invalid input!"); 
       sclocal.nextLine();//throw away the invalid input so that we can await for the next input 
      } 
      System.out.println("Enter ID:"); 
     } 
    } 
} 
+0

好的,添加所有缺少的信息,可否请再次检查一次? – Johnny

+0

我重新安排了一些代码,并添加了一些注释来使事情顺利进行。在这个线程中有关于异常处理的另一个注释,所以我在代码中添加了一行代码,您需要让整个事情正常工作。 –

0

基于您的代码,你可能想要的东西更多类似这样的

while(true){ 
    try{ 
     id=sclocal.nextInt(); 
     if(listperformer.size() <= id || id < 0) { 
      System.out.println("Invalid input! 294"); 
     } 
     else { 
      idlist.add(listperformer.get(id)); 
      break; 
     } 
    } catch(InputMismatchException exception) { 
     System.out.println("Invalid input!"); 
    } 
} 
+0

不起作用,导致无尽的输出:“输入无效!294” – Johnny

1

我看到这里有两个问题。

  1. 首先是在第一抓你只打印一个消息到System.out这个你PROGRAMM后会继续正常运行,从而进入下一个id=sclocal.nextInt()。相反,您应该将函数留在catch子句中。
  2. 现在,在您的try-catch块后,您再次拨打id=sclocal.nextInt()。这次没有捕捉到可能的异常,同时从sclocal获取了全新的值。您可以修复这个问题,即删除该调用并将if-else子句移入您的try块。
0

您需要包含全部依赖于try-catch块中的异常抛出操作的代码。我将你的代码扩展到MWE(见下面的输出)。

import java.util.*; 

public class TryCatch 
{ 
    public static void main(String[] args) 
    { 
     Scanner sclocal = new Scanner(System.in); 
     List<Integer> listperformer = new ArrayList<>(Arrays.asList(1,2,3)); 
     List<Integer> idlist = new ArrayList<>(); 
     try 
     { 
      int id=sclocal.nextInt(); 
      if(listperformer.size() <= id || id < 0) 
      { 
       System.out.println("Invalid input! 294"); 
       return; 
      } 
      else 
      { 
       idlist.add(listperformer.get(id)); 
      } 
     } 
     catch(InputMismatchException exception) 
     { 
      System.out.println("Invalid input!"); 
     } 
    } 
} 

结果

Input: string 
Output: Invalid input! 

Input: 1 
Output: [2] 

Input: 5 
Output: Invalid input! 294 
+0

好吧,会做到这一点! – Johnny