Java 将字符串数组转换为字符数组以将其与另一个文本文件进行比较

Java 将字符串数组转换为字符数组以将其与另一个文本文件进行比较,java,string,char,compare,converter,Java,String,Char,Compare,Converter,给两个文件 随机字母.txt AABBBBB FLOWERS BACKGFD TOBEACH 字典.txt flowers to beach back 我需要用字典检查每个随机字母的组合,看看是否有共同之处。它可以是至少包含6个字符的单词,也可以是至少包含6个字符的两个单词。这将使它成为FLOWERS或TOBEACH 我很难弄清楚我需要做什么。我可以让它对7个字符的单词起作用,因为我使用了字符串。我知道我需要使用char才能工作 到目前为止,我所拥有的: public static void

给两个文件

随机字母.txt

AABBBBB
FLOWERS
BACKGFD
TOBEACH
字典.txt

flowers
to
beach
back
我需要用字典检查每个随机字母的组合,看看是否有共同之处。它可以是至少包含6个字符的单词,也可以是至少包含6个字符的两个单词。这将使它成为
FLOWERS
TOBEACH

我很难弄清楚我需要做什么。我可以让它对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]);
    }

}}
publicstaticvoidcompare(){
私有静态字符串stringToWrite2=“”;
私有静态字符串[]allWords=新字符串[2187];
私有静态字符串[]diction=新字符串[79765];
私有静态字符[][]测试=新字符[2187][7];
私有静态字符[][]test2=新字符[79765][7];
公共静态void main(字符串参数[])
试一试{
Scanner file1=新扫描仪(新文件(“random_letters.txt”);
Scanner file2=新扫描仪(新文件(“dictionary.txt”);
对于(int i=0;i<2187;i++){
allWords[i]=file1.next();
test[i]=allWords[i].toCharArray();
}
对于(int i=0;i<79765;i++){
措辞[i]=file2.next();
用语[i]=用语[i].toUpperCase();
test2[i]=措辞[i].toCharArray();
}
对于(int i=0;i<2187;i++){
对于(int j=0;j<79765;j++){
if(allWords[i].equals(用词[j])){
stringToWrite2+=措辞[j];
}
}
}
}捕获(IOE异常){
System.out.println(“找不到文件”);
} 
System.out.println(“------------------------”;
System.out.println(stringToWrite2);
对于(int i=0;i<6;i++){
对于(int j=0;j<7;j++)
System.out.println(test2[i][j]);
}
}}

这里有两个不同的任务:确定字典中是否有任何单词也是随机字母(长度>=6),以及确定字典中是否有任意两组单词,以便它们的并集是随机字母中的单词

让我们使用哈希集来存储,而不是使用数组,因为这里最常用的操作可能是.contains(…)。它还允许我们访问.retainal(…),这对于查找交点非常有用

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

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");
  } 
 }
}
private static HashSet allWords=new HashSet();
私有静态HashSet diction=newhashset();
公共静态void compare(){
试一试{
Scanner file1=新扫描仪(新文件(“random_letters.txt”);
Scanner file2=新扫描仪(新文件(“dictionary.txt”);
对于(int i=0;i<2187;i++){
allWords.add(file1.next());
}
对于(int i=0;i<79765;i++){
add(file2.next().toUpperCase());
}
//编译两种语言中的单词集
HashSet intersect=新HashSet();
intersect.addAll(allWords);
交叉。保留(措辞);
用于(字符串s:相交){
系统输出打印项次;
}
//对于随机字母中的每个单词,看看是否有一个单词是它的开头
HashSet-coupsinterspect=新HashSet();
for(字符串s:allWords){
for(字符串d:措辞){
如果(s.startsWith(d)){
//如果是这样,请再次检查措辞中的每个单词,看看是否有一个单词是剩余的
字符串余数=s.subString(d.length());
for(字符串d2:措辞){
if(d2等于(余数))
//如果是,请储存这个单词
对联件等添加件;
}
}
}
}
//打印这些结果
用于(字符串s:耦合器等){
系统输出打印项次;
}
}捕获(IOE异常){
System.out.println(“找不到文件”);
} 
}
}

到目前为止,您尝试了什么?请阅读。发布您的代码,请参阅。有了这些代码,您会更容易理解,谢谢!为循环迭代硬编码是一个非常糟糕的主意。。。如果从其中一个文件中删除一行,则可能会抛出错误。试着使用while循环并阅读下一行,只要它在那里。通过“随机字母的每个组合”,你的意思是随机字母中的字符串可以重新排序以匹配dict中的单词吗?如果是这样的话,根据随机字母中字符串的长度,您将看到大量的工作。@Mshnik不,它们必须保持相同的顺序