Reputation: 1
So I am trying to split my String input by /, -, and space, and within the dateConversion method, I am trying to call upon the third term in my String array called terms. If my array only has 2 elements, I receive an error, and I understand why; the issue is that even if I declare the third element of the array before I split my original input, the program still crashes. It should print out the last if statement instead.
Scanner in=new Scanner(System.in);
System.out.println(message);
String input=in.nextLine();
if(input.equals("quit"))
{
System.out.println("Bye!");
return null;
}
else
return input;
public static void dateConversion(String input){
String[] terms=new String[2];
terms[2].equals(null);
terms=input.split("-|/| ");
if(terms[2].equals(null))
System.out.println("Wrong format. Enter again.\n");
}
The program should either continue if the third term of the array exists (and it does just fine when I test it), but if it intentionally doesn't exist, the last if statement should print instead of the program crashing. Is there some other way I can declare terms[2] so it doesn't crash?
Upvotes: 0
Views: 42
Reputation:
If you declare an array with two spaces like you have done --> String terms = new String[2]. Then there will be tow spaces created: terms[0] and terms[1]. The indexing starts at 0, not 1.
Upvotes: 1