Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello. I am getting a String Index out of range error. Please helpme fix this pr

ID: 3617052 • Letter: H

Question

Hello. I am getting a String Index out of range error. Please helpme fix this problem. Thank you.

Code:

import java.util.*;
public class C4CSe
{
public static void main(String [] args)
{
    Scanner scan = new Scanner(System.in);
    boolean loop = true;
    while (loop)
    {
      System.out.println("Enter date(mm/dd/yyyy): ");
      String date = scan.next();
      date.trim();
      int slash1 = date.indexOf ("/");
      boolean loop2 = true;
      if (slash1 < 0)
      {
        System.out.println("youMUST have a slash");
        while (loop2)
        {
         System.out.println("Try again? Enter ONLY yes or no: ");
          String try1= scan.next();
          if(try1.compareTo("yes")==0)
          {
           System.out.println("Ok");
           break;
          }else if(try1.compareTo("no")==0)
          {
           System.out.println("Thank you");
           return;
          }else
          {
           System.out.println("You can ONLY enter yes or no");
          }
        }
      }
      String month1 = date.substring(0,slash1);
      int month =Integer.parseInt(month1);
      int slash2 = date.indexOf ("/",(slash1 + 1));
      boolean loop3 = true;
      if (slash2 < 0)
      {
        System.out.println("youMUST have a slash");
        while (loop3)
        {
         System.out.println("Try again? Enter ONLY yes or no: ");
          String try2= scan.next();
          if(try2.compareTo("yes")==0)
          {
           System.out.println("Ok");
           break;
          }else if(try2.compareTo("no")==0)
          {
           System.out.println("Thank you");
           return;
          }else
          {
           System.out.println("You can ONLY enter yes or no");
          }
        }
      }
    }
}
}

Explanation / Answer

please rate - thanks no longer gets the error, and I got rid of the infinite loops import java.util.*; public class untitled { public static void main(String [] args) {int slash1=0;     Scanner scan = new Scanner(System.in);     boolean loop = true;       System.out.println("Enter date(mm/dd/yyyy): ");       String date = scan.next();     while (loop)        {System.out.println("&&&&");          date.trim();          slash1 =date.indexOf ("/");         boolean loop2 =true;         if (slash1 < 0)            {             System.out.println("you MUST have a slash");             while (loop2)                   {                   System.out.println("Try again? Enter ONLY yes or no: ");                   String try1 = scan.next();                   if (try1.compareTo("yes")==0)                         {loop2=false;                          System.out.println("Ok");                          }                      elseif (try1.compareTo("no")==0)                          {                           System.out.println("Thank you");                           return;                          }                     else                          {                          System.out.println("You can ONLY enter yes or no");                          }                                }               }                   else                loop=false;                     if(loop)          {System.out.println("Enter date (mm/dd/yyyy): ");         date = scan.next();           }       }          String month1 = date.substring(0,slash1);       int month =Integer.parseInt(month1);       int slash2 = date.indexOf ("/",(slash1 + 1));       boolean loop3 = true;       if (slash2 < 0)       {         System.out.println("youMUST have a slash");         while (loop3)         {          System.out.println("Try again? Enter ONLY yes or no: ");           String try2= scan.next();           if(try2.compareTo("yes")==0)           {            System.out.println("Ok");            loop3=false;           }else if(try2.compareTo("no")==0)           {            System.out.println("Thank you");            return;           }else           {            System.out.println("You can ONLY enter yes or no");           }         }       }    } }