无尽的while循环问题与try / catch [英] Endless while loop problem with try/catch

查看:102
本文介绍了无尽的while循环问题与try / catch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个while循环,它应该捕获一个非int输入,并要求用户重新输入一个int。但是它只是无休止地循环错误消息。
有没有人知道为什么它不能让扫描仪第二次输入?

  while(!enteredInt) 
{
try
{
轨迹= input.nextInt();
enteredInt = true;
}
catch(异常e)
{
//用户没有键入一个整数
System.out.println(你没有输入一个有效的数字,重新进入你将建立你的城市的地方);
}
}


解决方案

您输入一个不是数字的令牌,调用nextInt不会将扫描仪移动到令牌之外。因此,它只是一遍又一遍地读取相同的标记。



以下是nextInt的文档: http://download.oracle.com/javase/6/docs/api/java/util/ Scanner.html#nextInt%28%29 (虽然相关行为实际上是在引用参数的版本中描述的,这里是: http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html# nextInt%28int%29



所以,给你一个简单的例子,让我们假设输入的字符串是1 2 foo的。我将添加一个X来显示扫描仪当前所在的位置。它开始X1 2 Foo。然后,首次调用 input.nextInt()。它返回1,现在我们在1 X2 Foo。然后第二次调用 input.nextInt()。它返回2,现在我们在1 2 XFoo。然后第三次调用 input.nextInt()。它不返回,而是抛出一个InputMismatchException,它也不会推进扫描仪。所以我们还在1 2 XFoo。所以当我们称之为第四或第五次时,同样的事情就会发生。



为了解决这个问题,一个 input.next() code> call将消耗下一个令牌,无论是什么。在这个电话之后,在我们的例子中,我们将在1 2 FooX



调用 input.next()也可以抛出异常。具体来说,如果扫描仪关闭(不应该在您的代码中发生),否则可能会抛出IllegalStateException(如果扫描仪已经到达输入结束,则不会发生NoSuchElementException)(如果您从键盘读入,则不会发生)假设你是)。但是为了安全起见,你应该抓住这些错误,并且摆脱循环或者不抓住它们,而是用你的方法来声明它们。


I have a while loop that is supposed to catch a non-int input and ask the user to re-enter an int. However it just endlessly loops the error message. Does anyone have an idea why its not not allowing scanner input the second time through?

  while(!enteredInt)
  {
      try
      {
          locus = input.nextInt();
          enteredInt = true;
      }
      catch (Exception e)
      {
          //user didn't type an integer
          System.out.println("You didn't enter a valid number, re-enter point where you will build your city");
      }
  }

解决方案

When you enter a token which is not a number, calling nextInt does not move the scanner beyond the token. As a result, it's just reading the same token over and over.

Here's the documentation for nextInt: http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextInt%28%29 (although the relevant behavior is actually described in the version which takes an argument, here: http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextInt%28int%29 )

So, to give you a quick example, let's assume that the input string is "1 2 Foo". I will add an X to show where the Scanner is currently positioned. It starts out "X1 2 Foo". Then the first call to input.nextInt() happens. It returns 1 and now we're at "1 X2 Foo". Then the second call to input.nextInt() happens. It returns 2 and now we're at "1 2 XFoo". Then the third call to input.nextInt() happens. It doesn't return, but rather throws an InputMismatchException and it also doesn't advance the Scanner. So we're still at "1 2 XFoo". So when we call it a fourth or fifth time, the same thing happens.

To solve this problem, an input.next() call will consume the next token, whatever it is. After this call, in our example, we would be at "1 2 FooX".

Note, however, that the call to input.next() can also still throw exceptions. Specifically, it can throw IllegalStateException if the Scanner is closed (which shouldn't happen in your code) or NoSuchElementException if the Scanner has reached the end of input (which won't happen if you're reading in from the keyboard, as I assume that you are). But just to be safe, you should either catch those errors and break out of the loop or not catch them and instead declare them as thrown by your method.

这篇关于无尽的while循环问题与try / catch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆