I have a string array that I want the user to associate with the number of a month:
package samsExperiments; import java.util.Scanner; import java.util.Arrays; import java.util.Random; import static samsExperiments.StaticMethodExample.*; import java.lang.StringBuilder; import java.text.DecimalFormat; import java.util.Arrays; import java.util.InputMismatchException; import customExceptions.IntegerOutOfRangeException; public class SamsExperimentsMain { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Welcome to the Month selector\n"); String[] monthName = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "November", "December"}; String choice = "y"; while(choice.equalsIgnoreCase("y")) { System.out.println("Enter a month number, between 1 and 12."); int monthNumber = input.nextInt(); //validate input if(monthNumber < 1 || monthNumber > monthName.length) { System.out.println("Invalid month number. Try again."); continue; } int monthIndex = monthNumber - 1; System.out.println("You selected " + monthName[monthIndex]); System.out.print("Do you want to continue? (y/n): "); choice = input.nextLine(); System.out.println(); } } }
However, without any error messages, the program got stuck here:
Welcome to the Month selector
Enter a month number, between 1 and 12.
2
You selected February
Do you want to continue? (y/n):
That wouldn't have anything to do with taking user input as a string instead of an integer, would it?