Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Intro Java: how to eliminate case sensitivity for user input here?

If the user capitalizes, say, Capuchin, it works fine, but I’d like to eliminate case sensitivity. In my very limited experience, I’d look to place ".equalsIgnoreCase()" somewhere, but I can’t figure out where it’s possible to do that, or something similar. Do I need to change the method entirely or is there a way to do it within this block?

    String[]validSpecies = { "Capuchin", "Guenon", "Macaque", "Marmoset"};
    boolean isValid;

    System.out.println("Enter monkey's species: ");
    String species = scanner.nextLine();
    isValid = Arrays.asList(validSpecies).contains(species);
    if (!isValid) {
        System.out.println("This species is not permitted");
        return;
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Normalize your list of valid species by storing them all in e.g. lowercase, and apply the same normalization on your user input. Regardless of the case of the user’s input, you’re checking whether a lowercase string (species.toLowerCase()) is within a set of lowercase valid species.

    String[]validSpecies = { "capuchin", "guenon", "macaque", "marmoset"};
    boolean isValid;

    System.out.println("Enter monkey's species: ");
    String species = scanner.nextLine();
    isValid = Arrays.asList(validSpecies).contains(species.toLowerCase());
    if (!isValid) {
        System.out.println("This species is not permitted");
        return;
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading