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");
}
}
}
}
}
}