簡體   English   中英

如何比較 ArrayList 和忽略大小寫的字符串?

[英]How can I compare ArrayList and String ignoring case?

我正在解決這個問題

我目前的工作代碼是這樣的:

// 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

示例消息.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 '忽略大小寫'中。 我搜索了 inte.net,但找不到解決方案。 我怎么解決這個問題?

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

所以你的 ArrayList 包含將調用字符串上的“等於”。 這就是為什么當大小寫不匹配時它返回 false 的原因。

幾種處理方法:

  1. 將 arrMessage 的所有字符串更改為大寫/小寫,並在比較時使用temp[i].toUpperCase() 或 temp[i].toLoweCase()
  2. String周圍創建一個包裝器並覆蓋 equals 方法以執行 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)");

這意味着按空格或(逗號和空格)分隔

但是隨后在 output b.txt 中您將丟失逗號。

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

將 arrMessage 中的所有單詞設為小寫:

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

比較時,讓你掃描的單詞小寫:

if (arrMessage.contains(temp[i].toLowerCase())) {

我正在解決這個問題

我目前的工作代碼是這樣的:

// 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 '忽略大小寫'中。 我搜索了互聯網,但找不到解決方案。 我怎么解決這個問題?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM