1

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;
        }
    }
}
letsintegreat
  • 3,328
  • 4
  • 18
  • 39
larrys
  • 13
  • 1
  • 3

2 Answers2

2
  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;
    }
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • It is giving me an error and wants to add this Boolean valid = false, where you have it at the beginning = true. If I add as suggested, it returns "Not valid code" on anything I enter. – larrys May 10 '18 at 13:02
  • @larrys what error ? I tried with IT4782 and it works for me , did you copy exactly my answer ? – azro May 10 '18 at 13:05
  • I started a new project and pasted what you provide, and thank you for the assistance. @private static Boolean XXXXX (for me line 19) error says illegal start of expression – larrys May 10 '18 at 13:11
  • Got it, Thank you. I figured out what I was doing incorrectly when I was pasting it back. – larrys May 10 '18 at 13:25
0

You are using too much line of code: Here what I do:

   private static boolean validateCode(String code,String validCode) {
        boolean b=true;
        if (code.length() != 6) {
            return false;
        }
        for(int i=0;i<code.length();i++){
            if(code.toLowerCase().charAt(i)!=validCode.toLowerCase().charAt(i) && i<2){
                System.out.println("Character at "+i+" position is not an "+ validCode.charAt(i));
                b= false;
            }
            if(Character.isDigit(code.charAt(i)) && i>2){
                System.out.println("Character at "+i+" is not a digit");
                b= false;
            }
        }
    return b;
    }
Kumar Harsh
  • 423
  • 5
  • 26
  • same error, illegal start of expression, I am sensing I am not doing something correctly on my end – larrys May 10 '18 at 13:18
  • Got it, Thank you. I figured out what I was doing incorrectly when I was pasting it back. – larrys May 10 '18 at 13:25