首页 > 解决方案 > 如何比较 ArrayList 和 String 忽略大小写?

问题描述

我正在解决这个问题

我目前的工作代码是这样的:

// Importing the required packages.

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class AbbreviationsDriver {

// Main method.

    public static void main(String[] args) throws Exception {

        File abbreviationFile = new File("abbreviations.txt");
        Scanner scanFile = new Scanner(abbreviationFile);
        // ArrayList to save the messages in the file.
        ArrayList arrMessage = new ArrayList();
        String line;

        // read until last line
        while (scanFile.hasNextLine()) {
            line = scanFile.nextLine();
            arrMessage.add(line);
        }

        // Input File Name
        System.out.print(" Enter the name of the original file :");
        Scanner scanner = new Scanner(System.in);
        String inputFileName = scanner.next();

        // Output File Name
        System.out.print("\n Enter the name of the new file :");
        String outputFileName = scanner.next();

        Scanner scanInputFile = new Scanner(new File(inputFileName));
        PrintWriter out = new PrintWriter(new File(outputFileName));

        // Getting the line separator for the System.
        String newLine = System.getProperty("line.separator");

        while (scanInputFile.hasNextLine()) {
            String t;
            line = scanInputFile.nextLine();
            // Splitting on the basis of spaces.
            String[] temp = line.split(" ");
            // Adding the names to the ArrayList.
            for (int i = 0; i < temp.length; i++) {
                // if it is abbreviation, add <>
                if (arrMessage.contains(temp[i])) {
                    t = "<" + temp[i] + ">";
                }
                // if not, pass
                else {
                    t = temp[i];
                }
                // Write the string in the new file.
                out.write(t + " ");
            }
            // write new line
            out.write(newLine);
        }
        out.close();
    }
}

缩写.txt:

lol
:)
iirc
4
u
ttfn

sample_msg.txt

How are u today? Iirc, this is your first free day. Hope you are having fun! :)

但是当我尝试我的代码时,

How are <u> today? Iirc, this is your first free day. Hope you are having fun! <:)> 

显然,它没有过滤“Iirc”,因为它是大写的。但是,我想检查 String 是否在 ArrayList '忽略案例'中。我搜索了互联网,但找不到解决方案。我怎么解决这个问题?

标签: java

解决方案


List<T>.contains(T ..)在内部调用 T 上的“equals”方法。

因此,您的 ArrayList 包含将调用字符串上的“等于”。这就是为什么当大小写不匹配时它返回 false 的原因。

处理这种情况的几种方法:

  1. 将 arrMessage 的所有字符串更改为大写/小写,并在比较时使用temp[i].toUpperCase() 或 temp[i].toLowerCase()
  2. 在String周围创建一个包装器并覆盖 equals 方法以执行等于忽略大小写。

编辑:更多关于方法2

public class MyCustomStringWrapper {

  private String delegate;

  public MyCustomStringWrapper(String delegate) {
    this.delegate = delegate;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;
    MyCustomStringWrapper that = (MyCustomStringWrapper) o;
    return delegate.equalsIgnoreCase(that.delegate);
  }

  @Override
  public int hashCode() {
    return Objects.hash(delegate);
  }
}
public class AbbreviationsDriver {

// Main method.

    public static void main(String[] args) throws Exception {

        File abbreviationFile = new File("abbreviations.txt");
        Scanner scanFile = new Scanner(abbreviationFile);
        // ArrayList to save the messages in the file.
        List<MyCustomStringWrapper> arrMessage = new ArrayList();
        String line;

        // read until last line
        while (scanFile.hasNextLine()) {
            line = scanFile.nextLine();
            arrMessage.add(new MyCustomStringWrapper(line));
        }

        // Input File Name
        System.out.print(" Enter the name of the original file :");
        Scanner scanner = new Scanner(System.in);
        String inputFileName = scanner.next();

        // Output File Name
        System.out.print("\n Enter the name of the new file :");
        String outputFileName = scanner.next();

        Scanner scanInputFile = new Scanner(new File(inputFileName));
        PrintWriter out = new PrintWriter(new File(outputFileName));

        // Getting the line separator for the System.
        String newLine = System.getProperty("line.separator");

        while (scanInputFile.hasNextLine()) {
            String t;
            line = scanInputFile.nextLine();
            // Splitting on the basis of spaces.
            String[] temp = line.split(" ");
            // Adding the names to the ArrayList.
            for (int i = 0; i < temp.length; i++) {
                // if it is abbreviation, add <>
                if (arrMessage.contains(new MyCustomStringWrapper(temp[i]))) {
                    t = "<" + temp[i] + ">";
                }
                // if not, pass
                else {
                    t = temp[i];
                }
                // Write the string in the new file.
                out.write(t + " ");
            }
            // write new line
            out.write(newLine);
        }
        out.close();
    }
}

编辑:好的,这是输入文本/代码的另一个问题。

临时数组是通过用 ' '(空格)分割 sample_message 来创建的,即它包含一个名为“Iirc”的字符串,而不是“Iirc ”

因此,您还需要将 sample_msg.txt 文件更改为:

How are u today? Iirc , this is your first free day. Hope you are having fun! :)

由于您无法更改 sample_msg.txt 文件,因此您可以像这样更改拆分逻辑:

String[] temp = line.split("(\\s|,\\s)");

这意味着按空格或(逗号和空格)分隔

但是在输出 b.txt 中,您将丢失逗号。

How are <u> today? <Iirc> this is your first free day. Hope you are having fun! <:)> 

推荐阅读