So I am kinda new to java and was given a program to do.

a. Create one or more arrays to hold the names of the months and the number of days in each month. Assume that February always has 28 days.

b. Display the contents of the array(s).

c. Ask the user for a number to represent the month: 1 = Jan, 2 = Feb, etc. Search for the corresponding month and display the name of the month and the number of days. If the number is not found, display an error message.

d. Ask the user for the name of a month. Search for the corresponding month and display the number of the month and the number of days. If the month is not found, display an error message.

I have done a-c but i am having trouble with d.

package array;
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
    {
        int [] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Array holds number of days in month
        String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; // array holds months
       

        for (int index = 0; index < months.length; index++) // loop to display number of days in month
        {
         System.out.println(months[index] + " has " + // displays number of days in month
                            days[index] + " days ");
        }

        Scanner keyboard = new Scanner(System.in); // creates scanner
        System.out.println("Enter a number to represent the month");
        int search = keyboard.nextInt(); // saves user value into search

        search--; // subtracts 1 from user input to account for the index of 0

        if(search > months.length)

            System.out.println("Can not find number"); // yes if search is more then months display error
        else

            System.out.println( months[search] + " has " + days[search] + " days "); // no if search is less then months display contents

       
        Scanner string = new Scanner(System.in); //creates new scanner
        System.out.println("Enter the name of the month");
        String input = string.nextLine(); // saves user strin in to input

       for (int index = 0; index < months.length; index++) //loop to compare user input to months array
       {
        if(input.equals(months[index])) // yes display month and days
           System.out.println(months[index] + " has " + // displays number of days in month
                              days[index] + " days ");
        else
            if(!(input.equals(months[index]))) // no display error
            System.out.println("can not find month");
       }
    }

}

Its kinda hard to explain the problem but if you run it you will see when it asks the user to input the name of the month that is where the error occurs. Any help would be appreciated.

Ok, you can see that your search is basically working because it does print out the days for the month that matches.

The problem is that you are printing out something for every single iteration of the loop. You really only want to print out the result of your search. To do that, perhaps you should make a variable to hold the search results you want to display. If you find a match, set the variable to that information.

After you have completed the search loop, print out your result.

Also a hint goes from your teacher you may want to use only one array like

Month[] months = {new Month(31, "January")
                , new Month(28, "February")
                , new Month(31, "March")
                , new Month(30, "April")
                , new Month(31, "May")
                , new Month(30, "June")
                , new Month(31, "July")
                , new Month(31, "August")
                , new Month(30, "September")
                , new Month(31, "October")
                , new Month(30, "November")
                , new Month(31, "December")
        };


        for (Month each : months) // loop to display number of days in month
        {
            each.printMonth();
        }

with inner class

static class Month {
        private int days;
        private String month;

        public Month(int days, String month) {
            this.days = days;
            this.month = month;
        }

        public int getDays() {
            return days;
        }

        public String getMonth() {
            return month;
        }

        public void printMonth() {
            System.out.println(month + " has " + // displays number of days in month
                    days + " days ");
        }
    }

Solved it, just had to add a boolean value and move the error statement outside the for loop.

This question has already been answered. Start a new discussion instead.