简体   繁体   中英

Validating primitive data type

I was reviewing this data validation method Why does the printed output below return World! as true ? World! is not of type 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();
}

EDIT: I am trying to test each token as double, the method above is misleading and checks every other value. I just don't know enough java yet to complete the test:

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());

}

Output should be

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

It's true because the

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

is getting "Next" value after "World", it is 3. So the result would be true. I think you should check the value when you get it out. For example:

    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());

        }

Hope this help.

Let's step through this, pretending we're a computer.

  1. while (scanner.hasNext()) {

    Is there a next token? Yes (it's "Hello") so we execute the loop body.

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

    Reads the next token ("Hello") and prints "Hello"

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

    Is the next token a double? No (it's "World"), so hasNextDouble returns false, so prints "false".

  4. End of the loop, so restart.

  5. while (scanner.hasNext()) {

    Is there a next token? Yes (it's "World!") so we execute the loop body.

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

    Reads the next token ("World!") and prints "World!"

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

    Is the next token a double? Yes (it's "3") so hasNextDouble returns true, so prints "true".

  8. End of the loop, so restart.

(so far we've printed Hello , false , World! , true )

And so on. The computer is doing exactly what you told it to - I don't see a problem here.

I recommend changing your while loop to this:

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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