javaignore-case

How to compare Strings ignoring cases in Java


Newbie here I am trying to compare the brand and display to an array of Strings. Seems to be working now but I don't know how to make the comparison case-insensitive. All the options I found so far is to compare a string to another string. There is any way I can make that comparison? Right now only accept the values as stated in the array of strings.

P.S. This was an existing homework that our instructor wanted us to build on it, hence why I am using the "isValid" methods for validation.

Thanks!

    import com.entertainment.Television;
    import java.util.Arrays;
    import java.util.Scanner;
    
    class TelevisionConsoleClient {
    
        private static final Scanner scanner = new Scanner(System.in);
    
        public static void main(String[] args) {
            welcomeMessage();
        }
    
        public static void welcomeMessage() {
    
            //Welcome message to buyer
            System.out.println("Welcome to Our Online Ordering System.");
            System.out.println("Please answer the questions below to submit your order.");
    
            String brand = brandChoice();
            String display = displayChoice();
            int size = sizeChoice();
    
            System.out.println("Thank you. The television you ordered is: ");
            television(brand, display, size);
    
            //close scanner
            scanner.close();
        }
    
    
        public static String brandChoice() {
            String brandChoice = null;
            boolean hasBrand = false;
    
            while (!hasBrand) {
    
                System.out.println("Please enter the desired brand " + Arrays.toString(Television.VALID_BRANDS) + ":");
                brandChoice = scanner.nextLine();
    
                if (Television.isValidBrand(brandChoice))
                    hasBrand = true;
                else
                    System.out.println("Sorry " + brandChoice + " is not a valid brand");
            }
            return brandChoice;
        }
    
        private static String displayChoice() {
            String displayChoice = null;
            boolean hasDisplay = false;
    
            while (!hasDisplay) {
    
                System.out.println("Please enter the desired display type " + Arrays.toString(Television.VALID_DISPLAY) + ":");
                displayChoice = scanner.nextLine();
    
                if (Television.isValidDisplay(displayChoice))
                    hasDisplay = true;
                else
                    System.out.println("Sorry " + displayChoice + " is not a valid display type");
            }
            return displayChoice;
        }
    
        private static int sizeChoice() {
            Integer sizeChoice = null;
            boolean hasSize = false;
    
            while (!hasSize) {
    
                System.out.println("Please enter the desired size " + Arrays.toString(Television.VALID_SIZES) + ":");
                sizeChoice = Integer.parseInt(scanner.nextLine());
    
                if (Television.isValidSize(sizeChoice))
                    hasSize = true;
                else
                    System.out.println("Sorry " + sizeChoice + " is not a valid size");
            }
            return sizeChoice;
        }
    
        private static void television(String brand, String display, int size) {
            System.out.println(new Television(brand, display, size));
        }
    
    }


package com.entertainment;

public class Television {

    // CLASS OR STATIC VARIABLES - STORED IN THE SHARED AREA ASSOCIATED WITH A CLASS
    public static final String[] VALID_BRANDS = {"Samsung", "LG", "Sony", "Toshiba"};
    public static final String[] VALID_DISPLAY = {"LED", "OLED", "PLASMA", "LCD", "CRT"};
    public static final int[] VALID_SIZES = {32, 40, 43, 50, 55, 60, 65, 70, 75, 80};

    // FIELDS - AKA 'INSTANCE VARIABLES', 'ATTRIBUTES', 'PROPERTIES'
    private String brand;
    private String display;
    private int size;


    // CONSTRUCTORS
    // No-arg constructor.
    public Television() {
        // possible additional "setup" or initialization code here
        // want it to run for every instance created
    }

    // 3-arg constructor
    public Television(String brand, String display, int size) {
        this.brand = brand;
        this.display = display;
        this.size = size;
    }


    // ACCESSOR METHODS (getters/setters)

    public String getBrand() {
        return brand;
    }

    public String getDisplay() {
        return display;
    }

    public int getSize() { return size; }


    public static boolean isValidBrand(String brand) {
        boolean isValid = false;

        for (String currentBrand : VALID_BRANDS) {
            if (currentBrand.equals(brand)) {
                isValid = true;
                break;
            }
        }
        return isValid;
    }

    public static boolean isValidDisplay(String display) {
        boolean isValid = false;

        for (String currentDisplay : VALID_DISPLAY) {
            if (currentDisplay.equals(display)) {
                isValid = true;
                break;
            }
        }
        return isValid;
    }

    public static boolean isValidSize(int size) {
        boolean isValid = false;

        for (int currentSize : VALID_SIZES) {
            if (currentSize == size) {
                isValid = true;
                break;
            }
        }
        return isValid;
    }

    public String toString() {
        return "Television: " + getBrand() + ", Display: " + getDisplay() + ", Size: " + getSize() + " inches.";
    }
}

Solution

  • Change String.equals(Object) to String.equalsIgnoreCase(String). That is,

    if (currentBrand.equals(brand))
    if (currentDisplay.equals(display))
    

    to

    if (currentBrand.equalsIgnoreCase(brand))
    if (currentDisplay.equalsIgnoreCase(display))