Q:

How to fix "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5" problem in Java

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)

A:

There are two problems with the code. First, you should compare Strings using equals, not ==. Otherwise it can return false even for identical Strings.
Second, nextLine will read from the Scanner up until after the next newline. But nextInt only will read up until before that newline, so your first call to nextLine simply advances the Scanner by one character and returns an empty String. You need to call an extra nextLine after nextInt to advance onto the next line after your integer. For more information, see this question.
So you should make the following changes to your code:
Scanner scanner = new Scanner(new File("submit.txt"));

int trials = scanner.nextInt();
scanner.nextLine(); //Add this line, to consume the newline character after "5"
// alternatively, you could replace both of the above lines with this:
// int trials = Integer.parseInt(scanner.nextLine());
int total = 0;

for(int x = 0; x <= trials; x++){
   String sub = scanner.nextLine();
   sub = sub.toLowerCase();
   if(sub.substring(0,5).equals("today")) //compare strings using equals, not ==
     System.out.println("VALID ENTRY");
   else
     System.out.println("INCORRECT FORMATTING, TRY ANOTHER SUBMISSION");

}

