samedi 24 octobre 2020

how to allow integer only AND not allowing more than 9 digits

        System.out.println("Enter your phone number: ");
    while(in.hasNextLong()) {
        long phone = in.nextLong();
        if(in.hasNextLong()) {
            if(phone < 1000000000) {
                System.out.println("Phone number: "+phone); 
        }
    } else if(!in.hasNextInt()) {
        System.out.println("Please enter a valid phone number: ");
    } else if (phone < 1000000000) {
        System.out.println("Please enter a valid phone number: ");
    }
    

tried another way

        boolean valid;
    long phone;
    do {
        System.out.println("Enter your phone number: ");
        
        if(!in.hasNextLong()) {
            phone =in.nextLong();
            if(phone > 1000000000) {
            System.out.println("Please enter a valid phone number");
            in.nextLong();
            valid=false;
            }
        } 
        }while(valid=false);
    System.out.println("Phone: " + phone);

as you can see it doesnt work at all especially if you input a non integer and it ask for input twice im sorry its a mess

Aucun commentaire:

Enregistrer un commentaire