I'm trying to create a code that prints "VALID ENTRY" if the input begins with "Today" and ends with "MLIA" (case doesn't matter). If it doesn't, then it prints "INCORRECT FORMATTING, TRY ANOTHER SUBMISSION". For some reason, the program keeps giving me an out-of-bounds error and I can't figure out why.
When I changed the substring code to sub.substring(0,1)
to test it still gave me an error, so that is not the problem. I also tried adding the char value for each letter to determine what the word was but that didn't work either.
public class submit{
public static void main(String[] args) throws IOException{
Scanner scanner = new Scanner(new File("submit.txt"));
int trials = scanner.nextInt();
int total = 0;
for(int x = 0; x <= trials; x++){
String sub = scanner.nextLine();
sub = sub.toLowerCase();
if(sub.substring(0,5) == "today") //I only have it set up to find "today"
System.out.println("VALID ENTRY");
else
System.out.println("INCORRECT FORMATTING, TRY ANOTHER SUBMISSION");
}
}//end of main
}//end of class
Input:
5
ToDAY, I went to school. mlia
Hehehe today mlia this shouldn't work
Today, I went to a programming contest. Hehe. MLIA
TODAYMLIA
T0day is a brand new day! MLIA
Expected output should be:
VALID ENTRY
INCORRECT FORMATTING, TRY ANOTHER SUBMISSION
VALID ENTRY
VALID ENTRY
INCORRECT FORMATTING, TRY ANOTHER SUBMISSION
Actual output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.substring(String.java:1963)
at submit.main(submit.java:15)