Java task Validate "course code" with letters and numbers

My question relates to an assignment that I have already turned in--but I want to figure out based on what I coded (which does some of the requirements) where I failed, what I did correctly, and if there was a better approach. Seriously novice to be clear.

I've read through multiple previously asked questions and tried to put together information from those.

The task is to have the user input a six digit code--must meet these specifications:

  1. have an upper or lower case 'I' as the first
  2. have an upper or lower case 'T' as the second
  3. the third through sixth places must be digits 0-9

if the entered code matches--the return is "course code validated" of it doesn't the return is "course code not valid" with the reason it is not.

Pasting my code text below. Any feedback would be greatly appreciated.

import java.util.Scanner; 
public class U4A1_sixthSession {


    public static void main(String[] args) {
        System.out.println("Amber's Copy");
        Scanner input = new Scanner(System.in);

        System.out.println("Enter a course code to validate (e.g. IT4872):");
        String s = input.nextLine();


      if (s.charAt(0)!= 'i') {
    if (s.charAt(0)!=  'I') 
             System.out.println("Course Code must begin with the letter I." + "\n" +"Course code not valid.");

        else if (s.charAt(1)!=  't')
        if (s.charAt(1)!=  'T')
            System.out.println("Course Code must have second letter T." +"\n" + "Course code not valid.");

        else if (Character.isLetter(s.charAt(2))) 
            System.out.println("Course code must have digit in third spot." +"\n" + "Course code not valid.");
        else if (Character.isLetter(s.charAt(3))) 
            System.out.println("Course code must have digit in fourth place." +"\n" + "Course code not valid.");
        else if (Character.isLetter(s.charAt(4))) 
            System.out.println("Course code must have digit in fifth place." +"\n" + "Course code not valid.");
        else if (Character.isLetter(s.charAt(5))) 
            System.out.println("Course code must have digit in sixth place." +"\n" + "Course code not valid.");         
    else  
          System.out.println("Course code is valid."  + s);
    }
}
}

4 answers

  • answered 2018-01-11 21:11 LoganTippy

    One thing that stands out to me would be that for checking the input an improvement could be instead of using the double if statements you could do:

    if(String.valueOf(s.charAt(0)).equalsIgnoreCase('i'))
    
    if(String.valueOf(s.charAt(1)).equalsIgnoreCase('t'))
    

    what this does is it checks for the letter no matter its case.

    And for the numbers, you could do a for loop:

    for(int x = 2; x <= 5; ++x){
    
       if (Character.isLetter(s.charAt(x)))
    
           System.out.println(....);
    }
    

    These are purely improvements that would not only make your code work better, but also appear neater as well.

  • answered 2018-01-11 21:27 olmerg

    It is a better approach to test all the String throw functions like matches but that require you to know about regular expressions, it could be:

    import java.util.Scanner;
    public class Holatadeo2016 {
        public static void main(String[] args) {
    
           System.out.println("Amber's Copy");
            Scanner input = new Scanner(System.in);
    
            System.out.println("Enter a course code to validate (e.g. IT4872):");
            String s = input.nextLine();
    
            if(s.toLowerCase().matches("it[0-9][0-9][0-9][0-9]"))
                System.out.println("ok");
            else
                System.out.println("wrong");
        }
    }
    

  • answered 2018-01-11 21:46 ichantz

    Here is an alternative way to do it hope it is useful.

    public static void main(String[] args) {
                try {
                    System.out.println("Amber's Copy");
                    Scanner input = new Scanner(System.in);
    
                    System.out.println("Enter a course code to validate (e.g. IT4872):");
                    String s = input.nextLine();
    
                    boolean hasSixDigits = s.length() == 6;
                    boolean startsWithI = String.valueOf(s.charAt(0)).equalsIgnoreCase("i");
                    boolean secondLetterT = String.valueOf(s.charAt(1)).equalsIgnoreCase("t");
                    //will throw NumberFormatException if the 4 last chars are not numbers
                    Integer.parseInt(s.substring(2));
    
                    if (hasSixDigits && startsWithI && secondLetterT){
                        System.out.println("Valid course code");
                    }
                    else {
                        System.out.println("Invalid course code");
                        if (!hasSixDigits) {
                            System.out.println("Code should be a six digit code");
                        } 
                        if (!startsWithI) {
                            System.out.println("Code should start with I or i");
                        } 
                        if (!secondLetterT) {
                            System.out.println("Code's second letter should be T or t");
                        }
                    }
                }catch(NumberFormatException nfe){
                    System.out.println("Not valid third to last should be digits");
                }
    

  • answered 2018-01-11 21:54 Mumrah81

    From what i've seen your code is buggy, it failed to print valid/invalid for code starting with 'i'

    It also fail if you provide the following input "IT...." (.... = 4 spaces, they are not letters)

    Then what if there are more than one error? You only print the first one due to nested "if else else if".

    To check string java (and other languages offer a better approach using regular expressions, please see below:

    public static void main(String[] args) {
            System.out.println("Amber's Copy");
            Scanner input = new Scanner(System.in);
    
            System.out.println("Enter a course code to validate (e.g. IT4872):");
            String s = input.nextLine();
    
            Pattern pattern = Pattern.compile("^([i|I])(t|T)([0-9])([0-9])([0-9])([0-9])$");
            Matcher m = pattern.matcher(s);
    
            if (m.matches()) {
                System.out.println("Course code is valid." + s);
            } else {
                if (s.charAt(0) != 'i' && s.charAt(0) != 'I') {
                    System.out.println("Course Code must begin with the letter I.");
                }
                if (s.charAt(1) != 't' && s.charAt(1) != 'T') {
                    System.out.println("Course Code must have second letter T.");
                }
                if (!Character.isDigit(s.charAt(2))) {
                    System.out.println("Course code must have digit in third spot.");
                }
                if (!Character.isDigit(s.charAt(3))) {
                    System.out.println("Course code must have digit in fourth place.");
                }
                if (!Character.isDigit(s.charAt(4))) {
                    System.out.println("Course code must have digit in fifth place.");
                }
                if (!Character.isDigit(s.charAt(5))) {
                    System.out.println("Course code must have digit in sixth place.");
                }
                System.out.println("Course code not valid.");
            }
    
        }