Home > Blockchain >  How to get my scanner to recognize the words i have for it
How to get my scanner to recognize the words i have for it

Time:03-12

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);
    }
}

CodePudding user response:

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

CodePudding user response:

using if (mylist.contains(Choice)) worked! thank you so much

CodePudding user response:

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.

  • Related