mercredi 1 novembre 2017

My variable isnt matching the .length() method in my try block with my if statement [duplicate]

This question already has an answer here:

I dont know why my if statement in my first try block isnt working properly It's still printing out that it's not 9 digits when it is....I don't know whats wrong? I even printed it out to the console to check if its really 9 digits and it is so I dont know why it's resulting in saying it's not.

for (int i = 0; i < numEmployees; i++ ) {

        try {                                                                               //check SSN for correct format
            System.out.println("Please enter employee number " + i + " SSN: ");
            newSSN = s.next();
            SSNList.add(newSSN);

                if (newSSN.length() != 9); throw new Exception ("Not 9 digits "); 
                //else if (newSSN.substring(3,4) != "-"|| newSSN.substring(6,7) != "-") throw new Exception ("No hyphens");

    }
        catch (Exception SSNLengthException) {
            System.out.println(SSNLengthException.getMessage() + "you entered: " + newSSN);
            System.out.println("Please enter your SSN again: ");

            if (newSSN.length() == 9) {
                SSNList.add(newSSN);
            }
    }

Aucun commentaire:

Enregistrer un commentaire