I am making a program where the user is asked to enter a month in format mm/dd/yyyy I have to make sure the slash is in the 3rd and 6th positions. IF IT IS NOT in the correct formats, in position 3 AND 6 (according to instructions, but doesnt this imply its actualy 2 & 5), I must print "Incorrect Format" to the screen. Here is my code so far.
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.println("Enter a date (mm/dd/yyyy):");
String date = kbd.next();
String slash1 = date.substring(3,3);
String slash2 = date.substring(6,6);
int numofChar = date.length();
if (numofChar < 10)
{
System.out.println("Too few characters in the date");
}
if (numofChar > 10)
{
System.out.println("Too many characters in the date");
}
if (!(slash1.equals("/") || slash2.equals("/")))
{
System.out.println("incorrect format");
}
To be honest, I could have made the code look nicer, I have been playing and switching code for two hours on this one part of the problem, its just not making any sense to me. I have researched this question in different ways, just not coming up with the right way.
So if it seems messy or totally off track, that may be why. Also, ive likely confused everything i did know, so if something more is wrong, please help!
How would I declare a variable, that stores the character at position 3 and 6; then tests to see if both are in fact slashes?