I have to create a simple program that allows the user to input the date in the following format : month day, year and the program is supposed to output the date in this format: date-month-year. My code keeps giving me that my string index is out of bounds but I don't know how to fix it. Help please!
note that the ' ' is supposed to represent a spaceimport java.util.Scanner; public class UserDate { public static void main (String [] args){ Scanner input = new Scanner(System.in); String userDate = null; String month = null; String day = null; String year = null; int firstBlank = 0; int comma = 0; System.out.println("Please enter month day, year in that format"); userDate = input.nextLine(); firstBlank = userDate.indexOf(0,' '); comma = userDate.indexOf(' ', ','); month = userDate.substring(0,' '); day = userDate.substring(' ',comma); year = userDate.substring(comma); System.out.println(day + "-" + month + "-" + year); } }