Java,尝试用扫描仪捕捉
Java, try-catch with Scanner
我正在创建一个小算法,这是其中的一部分。
如果用户输入非整数值,我想输出一条消息让用户再次输入一个数字:
boolean wenttocatch;
do
{
try
{
wenttocatch = false;
number_of_rigons = sc.nextInt(); // sc is an object of scanner class
}
catch (Exception e)
{
wenttocatch=true;
System.out.println("xx");
}
} while (wenttocatch==true);
我遇到了一个永无止境的循环,我不明白为什么。
如何识别用户是否输入了一些非整数?
如果用户输入的是非整数,如何让用户重新输入?
更新
当我打印异常时我得到 'InputMismatchException',我应该怎么办?
任何时候你得到一个异常,wenttocatch
被设置为 true
并且程序将陷入无限循环。只要你没有得到异常,你就不会得到无限循环。
如果 sc.nextInt() 导致错误的逻辑是这样
1) wenttocatch 设置为 false
2) sc.nextInt() 抛出错误
3) wenttocatch 设置为 true
4) 重复[因为 wenttocatch 是真的]
解决这个问题在catch语句中设置wentocatch=false
catch (Exception e) {
wenttocatch=false;
System.out.println("xx");
}
如果你做的比这里显示的多,请使用计数器[如果你在计数,如果不是,则使用布尔值],但除非你做的更多,否则请执行上面的第一件事
boolean wenttocatch;
int count = 0;
do{
try {
wenttocatch=false;
number_of_rigons=sc.nextInt(); // sc is an object of scanner class
} catch (Exception e) {
count++;
wenttocatch=true;
System.out.println("xx");
}
}while(wenttocatch==true && count < 1);
回答评论:
我认为您希望在用户不再输入之前获取整数。根据您的输入,一种方法是这样做
int number_of_rigons;
while((number_of_rigons = sc.nextInt()) != null){
//do something
}
Scanner
在项目被阅读之前不会前进。 Scanner JavaDoc 中提到了这一点。因此,您可以使用 .next()
方法读取值或在读取 int
值之前检查是否 hasInt()
。
boolean wenttocatch;
int number_of_rigons = 0;
Scanner sc = new Scanner(System.in);
do {
try {
wenttocatch = false;
number_of_rigons = sc.nextInt(); // sc is an object of scanner class
} catch (InputMismatchException e) {
sc.next();
wenttocatch = true;
System.out.println("xx");
}
} while (wenttocatch == true);
你不必尝试捕捉。此代码将为您解决问题:
public static void main(String[] args) {
boolean wenttocatch = false;
Scanner scan = new Scanner(System.in);
int number_of_rigons = 0;
do{
System.out.print("Enter a number : ");
if(scan.hasNextInt()){
number_of_rigons = scan.nextInt();
wenttocatch = true;
}else{
scan.nextLine();
System.out.println("Enter a valid Integer value");
}
}while(!wenttocatch);
}
....
try {
....
} catch (Exception e) {
sc.next();
wenttocatch=true;
System.out.println("xx");
}
你可以简单地使用 hasNext(); java 中的方法,而不是 try and catch。
hasNext() 是 Java 扫描器 class 的一种方法,如果此扫描器的输入中有另一个标记,则 returns 为真。
String[] stringArray = new String[lines];
int i = 0;
try (Scanner sc = new Scanner(file)) {
while (sc.hasNextLine()) {
String data=sc.nextLine();
stringArray[i] = data;
i++;
}
} catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening file.");
throw new FileNotFoundException();
} catch (NoSuchElementException e) {
System.err.println("Error in file record structure");
throw new NoSuchElementException();
} catch (Exception e) {
e.printStackTrace();
}
/*在 try im new 编程中添加了扫描仪声明,它对我有用,但我不知道它是否是好的做法
*/ :
boolean wenttocatch;
do
{
try
{
Scanner sc=new Scanner(System.in);
wenttocoatch = false;
number_of_rigons = sc.nextInt();
}
catch (Exception e)
{
wenttocatch=true;
System.out.println("xx");
}
} while (wenttocatch==true);
我正在创建一个小算法,这是其中的一部分。
如果用户输入非整数值,我想输出一条消息让用户再次输入一个数字:
boolean wenttocatch;
do
{
try
{
wenttocatch = false;
number_of_rigons = sc.nextInt(); // sc is an object of scanner class
}
catch (Exception e)
{
wenttocatch=true;
System.out.println("xx");
}
} while (wenttocatch==true);
我遇到了一个永无止境的循环,我不明白为什么。
如何识别用户是否输入了一些非整数?
如果用户输入的是非整数,如何让用户重新输入?
更新
当我打印异常时我得到 'InputMismatchException',我应该怎么办?
任何时候你得到一个异常,wenttocatch
被设置为 true
并且程序将陷入无限循环。只要你没有得到异常,你就不会得到无限循环。
如果 sc.nextInt() 导致错误的逻辑是这样
1) wenttocatch 设置为 false
2) sc.nextInt() 抛出错误
3) wenttocatch 设置为 true
4) 重复[因为 wenttocatch 是真的]
解决这个问题在catch语句中设置wentocatch=false
catch (Exception e) {
wenttocatch=false;
System.out.println("xx");
}
如果你做的比这里显示的多,请使用计数器[如果你在计数,如果不是,则使用布尔值],但除非你做的更多,否则请执行上面的第一件事
boolean wenttocatch;
int count = 0;
do{
try {
wenttocatch=false;
number_of_rigons=sc.nextInt(); // sc is an object of scanner class
} catch (Exception e) {
count++;
wenttocatch=true;
System.out.println("xx");
}
}while(wenttocatch==true && count < 1);
回答评论:
我认为您希望在用户不再输入之前获取整数。根据您的输入,一种方法是这样做
int number_of_rigons;
while((number_of_rigons = sc.nextInt()) != null){
//do something
}
Scanner
在项目被阅读之前不会前进。 Scanner JavaDoc 中提到了这一点。因此,您可以使用 .next()
方法读取值或在读取 int
值之前检查是否 hasInt()
。
boolean wenttocatch;
int number_of_rigons = 0;
Scanner sc = new Scanner(System.in);
do {
try {
wenttocatch = false;
number_of_rigons = sc.nextInt(); // sc is an object of scanner class
} catch (InputMismatchException e) {
sc.next();
wenttocatch = true;
System.out.println("xx");
}
} while (wenttocatch == true);
你不必尝试捕捉。此代码将为您解决问题:
public static void main(String[] args) {
boolean wenttocatch = false;
Scanner scan = new Scanner(System.in);
int number_of_rigons = 0;
do{
System.out.print("Enter a number : ");
if(scan.hasNextInt()){
number_of_rigons = scan.nextInt();
wenttocatch = true;
}else{
scan.nextLine();
System.out.println("Enter a valid Integer value");
}
}while(!wenttocatch);
}
....
try {
....
} catch (Exception e) {
sc.next();
wenttocatch=true;
System.out.println("xx");
}
你可以简单地使用 hasNext(); java 中的方法,而不是 try and catch。 hasNext() 是 Java 扫描器 class 的一种方法,如果此扫描器的输入中有另一个标记,则 returns 为真。
String[] stringArray = new String[lines];
int i = 0;
try (Scanner sc = new Scanner(file)) {
while (sc.hasNextLine()) {
String data=sc.nextLine();
stringArray[i] = data;
i++;
}
} catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening file.");
throw new FileNotFoundException();
} catch (NoSuchElementException e) {
System.err.println("Error in file record structure");
throw new NoSuchElementException();
} catch (Exception e) {
e.printStackTrace();
}
/*在 try im new 编程中添加了扫描仪声明,它对我有用,但我不知道它是否是好的做法 */ :
boolean wenttocatch;
do
{
try
{
Scanner sc=new Scanner(System.in);
wenttocoatch = false;
number_of_rigons = sc.nextInt();
}
catch (Exception e)
{
wenttocatch=true;
System.out.println("xx");
}
} while (wenttocatch==true);