如何在Java中比较忽略大小写的字符串

黛比

在这里,我是新手,我正在尝试比较品牌并将其显示为一组字符串。似乎现在可以正常工作,但是我不知道如何使比较不区分大小写。到目前为止,我找到的所有选项都是将一个字符串与另一个字符串进行比较。有什么办法可以进行比较?现在,仅接受字符串数组中所述的值。

PS:这是我们老师希望我们以此为基础的现有作业,因此为什么我要使用“ isValid”方法进行验证。

谢谢!

    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.";
    }
}
艾略特新鲜

更改String.equals(Object)String.equalsIgnoreCase(String)那是,

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何比较忽略大小写的字符串

在哈希图中比较字符串与键值时忽略大小写

如何在字符串比较中忽略大小写

Java流排序与字符串比较忽略大小写

Solr:如何在字段类型“字符串”中搜索忽略大小写的记录?

如何在Visual Studio中比较两个文件而忽略大小写

将字符串与字符串数组进行比较,忽略大小写

如何在Angular中检查字符串是否包含忽略大小写的子字符串

在Java中,如何检查字符串是否包含子字符串(忽略大小写)?

如何在Spark RDD中比较不区分大小写的字符串?

替换字符串并忽略大小写Python

字符串包含-忽略大小写

比较两个字符串忽略大小写的最佳方法

C# 字符串比较忽略大小写敏感 HTML 标签

角度2/4字符串比较(忽略大小写)

如何在排序和忽略大小写的同时保留两个“相等”字符串的行顺序?

在 C++ 中比较两个字符串时如何忽略文本大小写?

在忽略大小写的情况下比较字符串的有效方法是什么?

排序字符串忽略大小写;大写优先的相等字符串

字符数组比较忽略大小写

比较时是否忽略字符串的大小写?

检查字符串是否包含忽略大小写的字符

如何通过多个用户定义的分隔符拆分字符串,忽略大小写和顺序

如何比较原始类型中的字符忽略大小写

SQL-在搜索字符串时忽略大小写

存储字符串并忽略大小写的数据结构

检查字符串是否包含忽略大小写的数组中的元素(JavaScript)

使用lodash从角度8的列表中删除重复的字符串(忽略大小写)

R:查找包含精确字符串匹配后跟_(忽略大小写)的文件