我的代码的这部分应该执行以下操作:
代码
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.
我们假设,我们的列表中只有三个条目。 结果:
Input: 5
Output:
Input: 4
Output: Invalid input! 294
Input: asdf
Output: Exception in thread "main" java.util.InputMismatchException...
答案 0 :(得分:1)
我在这里看到两个问题。
id=sclocal.nextInt()
。相反,您应该将该函数保留在catch子句中。id=sclocal.nextInt()
。这次没有从sclocal获取一个全新的值时捕获可能的异常。你可以解决这个问题,即删除该调用并将if-else子句移动到try块中。答案 1 :(得分: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:");
}
}
}
答案 2 :(得分: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!");
}
}
答案 3 :(得分: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