How to allow out of bounds index in Java

Tags: , , ,



I try to make the program so that the user can give input of the interger of the Month (e.g when user inputs number 4, The output should be April) and it will keep asking so long the user inputs a valid number (1 – 12). If user inputs an invalid number, the program should say “Invalid!”, then terminate. However, my program cannot follow through the while-loop and immediately set the invalid number as an exception. What should I do to make the program says “Invalid!”? Thanks!

    String[] months = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "Oktober", "November", "December"};
    Scanner input = new Scanner(System.in);


    int i = 1;
    while(i < 13 && i > 0)
    {

        if(i > 12 && i < 1)
        {
            break;
        }
        else
        {
            System.out.println("Month?");
            i = input.nextInt();
            System.out.println(months[i - 1] + "n");
        }
    }
    System.out.println("Invalid!");

Answer

You can use an infinite loop (i.e. while(true){}) and break it in case of InputMismatchException or the input integer out of the valid range (i.e. 1 to 12).

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July", "August",
                "September", "Oktober", "November", "December" };
        Scanner input = new Scanner(System.in);
        int i;
        while (true) {
            System.out.print("Month?");
            try {
                i = input.nextInt();
                if (i >= 1 && i <= 12) {
                    System.out.println(months[i - 1]);
                } else {
                    System.out.println("Invalid");
                    break;
                }
            } catch (InputMismatchException e) {
                System.out.println("Invalid");
                break;
            }
        }
    }
}

A sample run:

Month?10
Oktober
Month?1
January
Month?15
Invalid

Another sample run:

Month?12
December
Month?1
January
Month?abc
Invalid

By using java.time API:

import java.time.DateTimeException;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.print("Month?");
            try {
                Month month = Month.of(input.nextInt());
                // Change the locale as per your requirement e.g. Locale.ENGLISH
                System.out.println(month.getDisplayName(TextStyle.FULL, Locale.GERMAN));
            } catch (DateTimeException | InputMismatchException e) {
                System.out.println("Invalid");
                break;
            }
        }
    }
}

A sample run:

Month?10
Oktober
Month?1
Januar
Month?15
Invalid

Another sample run:

Month?10
Oktober
Month?1
Januar
Month?Abc
Invalid


Source: stackoverflow