简体   繁体   English

isDigit方法中的Character.isDigit参数和行不可用错误(说明)

[英]Character.isDigit parameters and line unavailable errors within the isDigit method (clarification)

I'm currently working on a small project for an introductory java class. 我目前正在为Java类入门小项目。 We're supposed to make a program which can take in an integer from the user and output the number of odds, evens, and zeroes present within the code. 我们应该编写一个程序,该程序可以从用户那里获取一个整数,并输出代码中存在的奇数,偶数和零的数量。 This seemed pretty easy to me, and I managed to implement the code, but a class mate, after I criticized his code for incorrectly following the prompt, noted that my code would crash if anything but digits was input. 这对我来说似乎很容易,我设法实现了代码,但是在我批评他的代码不正确地遵循提示后,一个同班同学指出,如果输入的不是数字,我的代码将会崩溃。

Out of spite I've tried to go beyond the prompt and have the program output an error message if it encounters characters aside from digits (instead of having my compiler return an error). 尽管如此,我仍然尝试超出提示范围,如果程序遇到除数字之外的字符,则使程序输出一条错误消息(而不是让我的编译器返回错误)。 However I'm returning multiple errors within the Eclipse compiler when using the isDigit method in the Character class. 但是,在Character类中使用isDigit方法时,我将在Eclipse编译器中返回多个错误。

I don't know exactly what's causing this, and I feel I must be missing something crucial, but my teacher quite frankly isn't qualified enough to understand what's causing the error, and none of my classmates can seem to figure it out either. 我不知道到底是什么原因造成的,而且我觉得我肯定缺少一些重要的东西,但是坦率地说,我的老师没有足够的资格去理解导致错误的原因,而且我的任何一个同学似乎都无法弄清楚。

package ppCH5;
import java.util.Scanner;

public class PP5_3
{
    public static void main(String[]args)
    {   
        int even = 0;
        int odd = 0;
        int zero = 0;
        int num = 0;
        int count = 0;

        boolean inputError = false;
        System.out.println("please provide some integer");
        Scanner scan = new Scanner(System.in);
        String numbers = scan.next();
        scan.close();
        Scanner intSeperate = new Scanner(numbers);
        intSeperate.useDelimiter("");

        while(intSeperate.hasNext())
        {
                if(Character.isDigit(numbers.charAt(count)))
                {
                    count++;
                    num = intSeperate.nextInt();
                    if((num % 2)==1)
                        odd++;
                    if((num % 2)==0)
                        if(num==0)
                            zero++;
                        else
                            even++;
                }
                else
                {
                    count++;
                    inputError = true;
            }
        }
        intSeperate.close();
        if(!inputError)
        {
            System.out.println("There are " + even + " even digits.\n" + odd     + " odd digits.\nAnd there are " + zero + " zeros in that integer.");
        }
        else
        {
            System.out.println("You have provided a disallowed input");
        }
    }
}

Any help would be appreciated, I'm currently at a loss. 任何帮助将不胜感激,我目前很茫然。

When you enter a single non-digit character, say a , the else branch inside the while loop will get executed, incrementing count , right? 当您输入一个非数字字符时,例如说a ,将执行while循环内的else分支,增加count ,对吗? And then the loop will start a new iteration, right? 然后循环将开始新的迭代,对吗?

In this new iteration, intSeparator.hasNext() still returns true. 在此新迭代中, intSeparator.hasNext()仍返回true。 Why? 为什么? Because the input a is never read by the scanner (unlike if you have entered a digit, intSeparator.nextInt would be called and would have consumed the input). 因为输入a不会被扫描程序读取(与您输入数字不同, intSeparator.nextInt将调用intSeparator.nextInt并消耗了输入)。

Now count is 1 and is an invalid index for the 1-character string. 现在count1并且是1个字符的字符串的无效索引。 Therefore, numbers.charAt(count) throws an exception. 因此, numbers.charAt(count)引发异常。

This can be avoided if you break; 如果您break;可以避免这种情况break; out of the loop immediately in the else block: 在else块中立即退出循环:

else
{
    inputError = true;
    break;
}

Also, don't close the scan scanner. 另外,请勿close scan仪。 scan is connected to the System.in stream. scan连接到System.in流。 You didn't open that stream, so don't close it yourself. 您没有打开该流,所以不要自己关闭它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM