I am attempting to build a program that validates that a users input following these rules:
First letter is an I or i; Second letter is T or t; Third, fourth, fifth and six characters must be digits & then display/output "code is valid" if all of these rules apply, and if not, explain each reason why in a separate line
When I run the program in Netbeans, it does display if the input is valid, but it ALWAYS displays: "first letter is not an I or an i", and "Second letter is not a T or a t", regardless of whether the input was an i and/or t.
package u4a1_validatecoursecode;
import java.util.Scanner;
/**
*
* @author kiarafauxbel
*/
public class U4A1_ValidateCourseCode {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create Scanner input
Scanner input = new Scanner (System.in);
//Receive input from user
System.out.print("Enter a course code to validate (e.g. IT4782): ");
// Declare Variables
String courseCode = input.nextLine();
char ch0 = courseCode.charAt(0);
char ch1 = courseCode.charAt(1);
char ch2 = courseCode.charAt(2);
char ch3 = courseCode.charAt(3);
char ch4 = courseCode.charAt(4);
char ch5 = courseCode.charAt(5);
//Declare rules
if (ch0 == ('i'));
if (ch1 == ('t'))
if (Character.isDigit(ch2))
if (Character.isDigit(ch3))
if (Character.isDigit(ch4))
if (Character.isDigit (ch5))
System.out.println("Course code: " + courseCode + " is valid.");
else
if (ch0 != ('I'));
System.out.println("First character is not an I or an i");
if (ch1 != 'T');
System.out.println("Second character is not a T or a t");
if (Character.isLetter(ch2))
System.out.println("Third character is not a digit");
if (Character.isLetter(ch3))
System.out.println("Fourth character is not a digit");
if (Character.isLetter(ch4))
System.out.println("Fifth character is not a digit");
if (Character.isLetter(ch5))
System.out.println("Sixth character is not a digit");
}
}
Any advice? Thanks in advance
Aucun commentaire:
Enregistrer un commentaire