【问题标题】:Converting an String array into char array to compare it with another text file [closed]将字符串数组转换为字符数组以将其与另一个文本文件进行比较[关闭]
【发布时间】:2014-10-01 08:14:38
【问题描述】:

给定两个文件

random_letters.txt

AABBBBB
FLOWERS
BACKGFD
TOBEACH

字典.txt

flowers
to
beach
back

我需要检查 random_letters 和字典的每个组合,看看是否有任何共同点。它可以是一个至少有 6 个字符的单词,也可以是两个至少等于 6 个字符的单词。这将使它成为FLOWERSTOBEACH

我很难弄清楚我需要做什么。因为我使用了字符串,所以我可以让它适用于 7 个字符的单词。我知道我需要使用 char 才能使其工作。

到目前为止我所拥有的:

public static void compare() {

private static String stringToWrite2 = "";
private static String[] allWords = new String[2187];
private static String[] diction = new String[79765];
private static char[][] test = new char[2187][7];
private static char[][] test2 = new char[79765][7];
  public static void main(String args[])
    try {
        Scanner file1 = new Scanner(new File("random_letters.txt"));
        Scanner file2 = new Scanner(new File("dictionary.txt"));

        for(int i = 0; i < 2187; i++) {
            allWords[i] = file1.next();
            test[i] = allWords[i].toCharArray();
        }


        for(int i = 0; i < 79765; i++) {
            diction[i] = file2.next();
            diction[i] = diction[i].toUpperCase();
            test2[i] = diction[i].toCharArray();
        }

        for(int i = 0; i < 2187; i++) {
            for (int j = 0; j < 79765; j++) {
                if(allWords[i].equals(diction[j])) {
                    stringToWrite2 += diction[j];
                }
            }
        }
    } catch (IOException e) {
        System.out.println("could not find file");
    } 
    System.out.println("-------------------");
    System.out.println(stringToWrite2);

    for(int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++)
            System.out.println(test2[i][j]);
    }

}}

【问题讨论】:

  • 到目前为止您尝试过什么?请阅读How do I ask a good question?。发布您的代码,请参阅How to create a Minimal, Complete, and Verifiable example
  • 代码更容易理解,谢谢!
  • 对这些 for 循环迭代进行硬编码是一个非常糟糕的主意......如果从其中一个文件中删除一行,则可能会引发错误。尝试使用 while 循环并读取下一行,只要它在那里。
  • 通过“random_letters 的每个组合”,您的意思是 random_letters 中的字符串可以重新排序以匹配 dict 中的单词吗?如果是这样,您需要做很多工作,具体取决于 random_letters 中字符串的长度。
  • @Mshnik 不,他们必须保持相同的顺序

标签: java string char compare converter


【解决方案1】:

您在这里有两个不同的任务:确定字典中是否有任何单词也在 random_letters(长度 >= 6)中,以及确定字典中是否有任何两个单词的集合,使得它们的并集是random_letters 中的单词。

我们不使用数组,而是使用 HashSets 进行存储,因为这里最常用的操作可能是 .contains(...)。它还使我们能够访问 .retainAll(...),这对于查找交叉点非常有用。

对于任务的后半部分,我最初的想法是创建一个包含所有单词成对排列的数据结构,并将其与 allWords 相交。我很快意识到(可能)会变得有多大。相反,我使用了一个更丑但更节省空间的解决方案。

private static HashSet<String> allWords = new HashSet<String>();
private static HashSet<String> diction = new HashSet<String>();

public static void compare() {
  try {
    Scanner file1 = new Scanner(new File("random_letters.txt"));
    Scanner file2 = new Scanner(new File("dictionary.txt"));

    for(int i = 0; i < 2187; i++) {
      allWords.add(file1.next());
    }

    for(int i = 0; i < 79765; i++) {
      diction.add(file2.next().toUpperCase());
    }

    //Compile set of words that are in both
    HashSet<String> intersect = new HashSet<String>();
    intersect.addAll(allWords);
    intersect.retainAll(diction);

    for (String s : intersect){
        System.out.println(s);
    }

    //For every word in random_letters, see if there is a word in diction that is the start of it
    HashSet<String> couplesIntersect = new HashSet<String>();
    for(String s : allWords){
        for(String d : diction){
            if(s.startsWith(d)){
                //If so, check every word in diction again to see if there is a word that is the remainder
                String remainder = s.subString(d.length());
                for(String d2 : diction){
                    if(d2.equals(remainder))
                        //If so, store this word
                        couplesIntersect.add(s);
                }
            }
        }
     }

     //Print those results
     for (String s : couplesIntersect){
         System.out.println(s);
     }

  } catch (IOException e) {
    System.out.println("could not find file");
  } 
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多