How can i compare int to a String Array?

3983 views java

Im trying to compare the user input which is int to a String array.

import java.util.Arrays;
import java.util.Scanner;

public class Array{

    public static void main(String[]args) {

    Scanner input = new Scanner(;

    int number;

    String [] month = new String[13];
    month[0] = "Null";
    month[1] = "January";
    month[2] = "February";
    month[3] = "March";
    month[4] = "April";
    month[5] = "May";
    month[6] = "June";
    month[7] = "July";
    month[8] = "August";
    month[9] = "September";
    month[10] = "October";
    month[11] = "November";
    month[12] = "December";

    System.out.println("Enter a number");
    number = input.nextInt();


answered question

You can convert your int to string before you comapre : Integer.toString(YOUR_INT_VAR)

You need to specify what you are asking for. It does not make sense to do Integer.CompareTo(String[])

'Compare' are you sure it's the good word ? Or from int input get the String month ?

1 Answer


You can just use the number as the index on your month array. Say they input 1 for January, then you can just use a simple index:

System.out.println("You've selected the month of: " + month[number]);

Will output "January" for month[number], you will also have to check for out of range, of course.

posted this

Have an answer?


Please login first before posting an answer.