Chi Shen
Chi Shen

Reputation: 207

how to validate txt scanner data in java

i want to validate the client ID for only having integer and do not duplicated. the txt file contain client details such as id,name and so on. public void loadClientData() {

    String fnm="", snm="", pcd="";
    int num=0, id=1;
    try {
        Scanner scnr = new Scanner(new File("clients.txt"));
        scnr.useDelimiter("\\s*#\\s*");
        //fields delimited by '#' with optional leading and trailing spaces  

        while (scnr.hasNextInt()) {
            id  = scnr.nextInt();
            snm = scnr.next();
            fnm = scnr.next();
            num = scnr.nextInt();
            pcd = scnr.next();

            //validate here type your own code

            if( id != scnr.nextInt()) {
                System.out.println("Invalid input! ");
                scnr.next();
            }   
        //some code...
        }
    }
}

it only print out the first client.

any help pls?

and also is it already validated?

this is the client.txt

0001#  Jones#          Jack#           41#   NX4 4XZ#
0002#  McHugh#         Hugh#           62#   SS3 3DF#
0003#  Wilson#         Jonny#          10#   LO4 4UP#
0004#  Heath#          Edward#         9#    LO4 4UQ#
0005#  Castle#         Brian#          15#   LO4 4UP#
0006#  Barber#         Tony#           11#   LO4 4UQ#
0007#  Nurg#           Fred#           56#   NE8 1ST#
0008#  Nurg#           Frieda#         56#   NE8 1ST#
0009#  Mouse#          Mickey#         199#  DD33 5XY#
0010#  Quite-Contrary# Mary#           34#   AB5 9XX#

Upvotes: 0

Views: 83

Answers (1)

vlatkozelka
vlatkozelka

Reputation: 999

From Java documentation for the Scanner class:

public int nextInt()

Scans the next token of the input as an int.

An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.

Returns: the int scanned from the input Throws:

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed

The function will throw an InputMismatchException if the scanner reads invalid input that doesn't match an Integer regular expression.

You can use that exception for validation using try - catch block and handle the "error" the way you want

You also have other problems in your code. I modified it and put the explanation in comments

public static void loadClientData() {



 try {
        String fnm = "", snm = "", pcd = "";
        int num = 0, id = 1;
        Scanner scnr = new Scanner(new File("clients.txt"));
        scnr.useDelimiter("\\s*#\\s*");
        //fields delimited by '#' with optional leading and trailing spaces
        //check for hasNext not hasNextInt since the next character might not be an integer
        while (scnr.hasNext()) {

            //here happens the integer validation

            try {
                id = scnr.nextInt();
            } catch (InputMismatchException ex) {
                System.out.println("Invalid input !");
            }
            System.out.print(id + "  ");
            snm = scnr.next();
            System.out.print(snm + "  ");
            fnm = scnr.next();
            System.out.print(fnm + "  ");
            num = scnr.nextInt();
            System.out.print(num + "  ");
            pcd = scnr.next();
            System.out.println(pcd + "  ");

            //you need to do this to get the scanner to jump to the next line
            if (scnr.hasNextLine()) {                    
                scnr.nextLine();
            }
            //validate here type your own code
            /* if (id != scnr.nextInt()) {
             System.out.println("Invalid input! ");
             //scnr.next();
             }*/

        }
    } catch (Exception ex) {

    }

}

Upvotes: 1

Related Questions