验证原始数据类型

时间:2015-09-21 01:39:17

标签: java validation types

我正在审核此数据验证method为什么下面的打印输出会将World!作为true返回? World!不是double类型

    public static void tutorials_Point(){
    String s = "Hello World! 3 + 3.0 = 6 ";
    double d = 1.3985;
    s=s+d;

    // create a new scanner with the specified String Object
    Scanner scanner = new Scanner(s);

    // assign locale as US to recognize double numbers in a string
//    scanner.useLocale(Locale.US);

    while (scanner.hasNext()) {
        // print what is scanned
        System.out.println("" + scanner.next());

        // check if the scanner's next token is a double
        System.out.println("" + scanner.hasNextDouble());

    }

    // close the scanner
    scanner.close();
}

编辑:我正在尝试将每个令牌测试为double,上面的方法会产生误导并检查其他所有值。我还没知道足够的java还没有完成测试:

while (scanner.hasNext()) {
    // print what is scanned
    String logical = scanner.next();
    System.out.println("Checking whether " + logical + " is of type 'double' ...");

    // check if the scanner's next token is a double
    System.out.println("" + scanner.hasNextDouble());

}

输出

Checking whether Hello is of type 'double' ...
false
Checking whether World! is of type 'double' ...
false
Checking whether 3 is of type 'double' ...
true
Checking whether + is of type 'double' ...
false
Checking whether 3.0 is of type 'double' ...
true
Checking whether = is of type 'double' ...
false
Checking whether 6 is of type 'double' ...
true
Checking whether 1.3985 is of type 'double' ...
true

3 个答案:

答案 0 :(得分:1)

这是真的,因为

 System.out.println("" + scanner.hasNextDouble()); 

在“世界”之后得到“下一个”值,它是3.所以结果是真的。 我认为你应该检查它的价值。例如:

    while (scanner.hasNext()) {
            // print what is scanned
            String currentValue = scanner.next();
            boolean isDouble = false;
            try {
                double doubleValue = Double.valueOf(currentValue);
                isDouble = true;
                System.out.println(doubleValue + " : " + isDouble);
            } catch(NumberFormatException ex) {
                System.out.println(currentValue + " : " + isDouble);
            }
            // check if the scanner's next token is a double
//          System.out.println("" + scanner.hasNextDouble());

        }

希望得到这个帮助。

答案 1 :(得分:0)

让我们一步一步,假装我们是一台电脑。

  1. while (scanner.hasNext()) {

    有下一个令牌吗?是(它"你好")所以我们执行循环体。

  2. System.out.println("" + scanner.next());

    读取下一个标记(" Hello")并打印" Hello"

  3. System.out.println("" + scanner.hasNextDouble());

    下一个令牌是双倍的吗?不(它"世界"),所以hasNextDouble返回false,因此打印" false"。

  4. 循环结束,重启。

  5. while (scanner.hasNext()) {

    有下一个令牌吗?是(它"世界!")所以我们执行循环体。

  6. System.out.println("" + scanner.next());

    读取下一个标记(" World!")并打印" World!"

  7. System.out.println("" + scanner.hasNextDouble());

    下一个令牌是双倍的吗?是(它" 3")所以hasNextDouble返回true,所以打印" true"。

  8. 循环结束,重启。

  9. (到目前为止,我们已打印HellofalseWorld!true

    等等。计算机正在按照你的说法完成 - 我在这里看不到问题。

答案 2 :(得分:0)

我建议将while循环更改为:

while (scanner.hasNext()) {
    // check if the scanner's next token is a double
    boolean isNextDouble = scanner.hasNextDouble();

    // print what is scanned
    String logical = scanner.next();

    System.out.println("Checking whether " + logical + " is of type 'double' ...");
    System.out.println("" + isNextDouble);

}