1

I'm pretty new to java and coding in general and am trying to figure out how to get this game to work for a project at school. It is meant for you to type in a month and it will then ask you to choose a day, however when I input a month it always just says its an invalid input, which is what I want it to do when it is not a valid month. What am I doing wrong?

import java.util.*;

class Main {
    public static void main(String[] args) {
        boolean game = true;
        do {
            System.out.println("Welcome to the Famous Date game!");
            System.out.println("Please choose a month");
            Scanner Month = new Scanner(System.in);
            String  Choice = Month.nextLine();
            String[] Months = {"January", "February", "March", "April", "May", "June","July",                    
"August","September","October","November", "December"};
            List<String> mylist = Arrays.asList(Months);
            if (Choice.equals(mylist)) {
                System.out.println("Please choose a day");
            }
            else
                System.out.println("That is not a valid month");
        }
        while (game=true);
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 3
    Try `mylist.contains(Choice)`. – Johannes Kuhn Mar 11 '22 at 14:40
  • 1
    unrelated to your issue: [java naming convention](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html) – jhamon Mar 11 '22 at 14:42
  • 3
    `Choice.equals(mylist)` This will never be true. It is checking whether a `String` is equal to a `List`. Two variables with different types can never be equal. – Abra Mar 11 '22 at 14:42
  • 1
    Minor quibble: your question title is quite confused since the Scanner isn't being used to recognize anything. It is only being used to capture input from the user. – DontKnowMuchBut Getting Better Mar 11 '22 at 14:51

2 Answers2

2

try to test if contains the month list.contains() and the days in an other methode just call it

  • Is there a way i am able to make it so the months are not case sensitive? – David Armstrong Mar 11 '22 at 15:41
  • @DavidArmstrong refer to method [toUpperCase](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#toUpperCase()) – Abra Mar 11 '22 at 16:05
0

If you are allowed to use the classes in the java.time package then you can use Month (which is an enum). That saves you from having to create your own list.

Also, after the user selects a month, she needs to select a day. I assume that the user is supposed to enter a valid day, for example entering the number 30 when the entered month is February would constitute an invalid day. For checking whether the entered day is valid, you can use class YearMonth which has a method for returning the number of days in each month and takes into consideration leap years. Refer to this SO question: Number of days in particular month of particular year?

Here is a SSCCE
(Notes after the code.)

import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.temporal.ChronoField;
import java.util.InputMismatchException;
import java.util.Scanner;

public class GameMain {

    public static void main(String[] args) {
        int year = LocalDate.now().get(ChronoField.YEAR);
        Scanner keyboard = new Scanner(System.in);
        boolean game = true;
        do {
            System.out.print("Please choose a month: ");
            String month = keyboard.nextLine();
            try {
                Month theMonth = Month.valueOf(month.toUpperCase());
                YearMonth daysInMonth = YearMonth.of(year, theMonth);
                do {
                    System.out.print("Please choose a day: ");
                    try {
                        String val = keyboard.nextLine();
                        int day = Integer.parseInt(val);
                        if (day < 0 || day > daysInMonth.lengthOfMonth()) {
                            System.out.printf("%s does not have %d days.%n", theMonth, day);
                        }
                        else {
                            game = false;
                        }
                    }
                    catch (NumberFormatException xNumberFormat) {
                        System.out.println("Please enter a number.");
                    }
                } while (game);
            }
            catch (IllegalArgumentException xIllegalArgument) {
                System.out.println("That is not a valid month");
            }
        } while (game);
    }
}

Note that you only need to create a Scanner once. In the code in your question, you are creating a Scanner in every iteration of the do-while loop.

Also, if you enter a value that does not exist in an enum, an IllegalArgumentException is thrown.
And method parseInt throws NumberFormatException if the entered value is not an integer.

Abra
  • 19,142
  • 7
  • 29
  • 41