Search code examples
javastringchars

Validate User Input Using Java Chars and Strings


I have seen this asked 2x, but the correct response I need has not been addressed.

In this assessment, you will design and code a Java console application that validates the data entry of a course code (like IT4782) and report back if the course code is valid or not valid. The application uses the Java char and String data types to implement the validation. You can use either the Toolwire environment or your local Java development environment to complete this assignment.

The requirements of this application are as follows: The application is to read a course code entered by the user from the keyboard. The course code is made of 5 characters and should follow these

Rules:

  • First character is always an upper case I or a lower case i
  • Second character is always an upper case T or a lower case t
  • Third, fourth, fifth, and sixth characters are always digits (0- 9) The application then validates the course code against above the rules and prints a message

If the course code is valid or not. If the course code is not valid, the application should print a message explaining why the course code is not valid.

Output should look like this: Output should look like this

Here is my code, I cannot get the code to produce the pictured results. It outputs all the invalid messages.

package u4a1_validatecoursecode;

import java.util.Scanner;

public class U4A1_ValidateCourseCode {

    public static void main(String[] args) {
        // Larry Copy
        Scanner s = new Scanner(System.in);
        System.out.print("Enter a course code to validate (e.g. IT4782) : ");
        String code = s.nextLine();
        if (validateCode(code)) {
            System.out.println("Course code: " + "" + code + "" + " is valid.");
        } else {
            System.out.println("Not valid code");
        }
    }

    private static boolean validateCode(String code) {
        if (code.length() != 6) {
            return false;
        } else {
            //First character is always an upper case I or a lower case i
            if (code.charAt(0) != 'I' && code.charAt(0) != 'i') {
                return false;
            }
            System.out.println("integer is not an I or i");

            // Second character is always an upper case T or a lower case t
            if (code.charAt(1) != 'T' && code.charAt(1) != 't') {
                return false;
            }
            System.out.println("integer is not a T or t");

            // Third, fourth, fifth, and sixth characters are always digits (0-9)
            if (!Character.isDigit(code.charAt(2))) {
                return false;
            }
            System.out.println("integer 3 is not a number");

            if (!Character.isDigit(code.charAt(3))) {
                return false;
            }
            System.out.println("integer 4 is not a number");

            if (!Character.isDigit(code.charAt(4))) {
                return false;
            }
            System.out.println("integer 5 is not a number");

            if (!Character.isDigit(code.charAt(5))) {
                return false;
            }
            System.out.println("integer 6 is not a number");
            return false;
        }
    }
}

Solution

    1. When you return false; the code after is not executed so you'll never see why it returns
    2. If you return only false the test will never pass, you need a variable to validate or not the code
    3. If it goes in one if (not valid) you'll get the message, and the valid will be false

    private static boolean validateCode(String code) {
        if (code.length() != 6) {
            return false;
        } else {
            boolean valid = true;
            //First character is always an upper case I or a lower case i
            if (code.charAt(0) != 'I' && code.charAt(0) != 'i') {
                System.out.println("integer is not an I or i");
                valid = false;
            }
            // Second character is always an upper case T or a lower case t
            if (code.charAt(1) != 'T' && code.charAt(1) != 't') {
                System.out.println("integer is not a T or t");
                valid = false;
            }
            // Third, fourth, fifth, and sixth characters are always digits (0-9)
            if (!Character.isDigit(code.charAt(2))) {
                System.out.println("integer 3 is not a number");
                valid = false;
            }
            if (!Character.isDigit(code.charAt(3))) {
                System.out.println("integer 4 is not a number");
                valid = false;
            }
            if (!Character.isDigit(code.charAt(4))) {
                System.out.println("integer 5 is not a number");
                valid = false;
            }
            if (!Character.isDigit(code.charAt(5))) {
                System.out.println("integer 6 is not a number");
                valid = false;
            }
            return valid;
        }
    }