【问题标题】:How do I load text from a text file and compare against another text file?如何从文本文件加载文本并与另一个文本文件进行比较?
【发布时间】:2013-07-20 06:33:47
【问题描述】:

我编写了一个小程序,它从文件中读取文本并打印文本文件中每个单词的反面。

现在我想尝试有效地查找所有反向的单词是否都是英语词典中的实际单词,并将它们打印为按字符串长度排序的列表,每对出现一次。

这是我目前能写的内容

    String word;
    String[] reverse;
    int wordLength;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException {
    // TODO code application logic here

    Map<Integer, String> aMap2 = new HashMap<Integer, String>();
    String mString = "";
    int mInt = 0;
    String word;
    String[] reverse = new String[1];
    int wordLength;
    
    FileInputStream fileIn = new FileInputStream ("example.txt”);
    //text in the text file - "what he saw was not part of a trap just a ton of crazy snow"
    Scanner scan = new Scanner(fileIn);
    Scanner lineScanner;
    Scanner wordscanner = null;
    String aLine;
    String aWord;

    while(scan.hasNextLine()){
        aLine = scan.nextLine();
        lineScanner = new Scanner(aLine);
        lineScanner.useDelimiter(",");
        word = lineScanner.next();
        System.out.println(word);
        try{
            wordscanner = new Scanner(new File("example.txt"));
        } catch(FileNotFoundException x){
            x.printStackTrace();
        }

        while(wordscanner.hasNext()){
            Scanner newWord = new Scanner(wordscanner.next());
            boolean b;
            while(b = newWord.hasNext()){
                String foundWord = newWord.next();
                wordLength = foundWord.length();
                char ch = ' ';
                String reverseWord = “";

                for(int i = wordLength-1; i >= 0; i--){
                    ch = foundWord.charAt(i);
                    //System.out.println(ch);
                    reverseWord = reverseWord + ch;
                }
                
                for(int k = 0; k < reverse.length; k++){
                      reverse[k] = foundWord + ", " + reverseWord;
                      mString = reverse[k];
                      mInt = reverse[k].length();
                      aMap2.put(mInt, mString);
                }
            }
        }
    }
    Map<Integer, String> sorted = sortByKeys(aMap2);
    System.out.println(sorted);
}


public static <K extends Comparable,V extends Comparable> Map<K,V> sortByKeys(Map<K,V> map){
    List<K> keys = new LinkedList<K>(map.keySet());
    Collections.sort(keys);
  
    //LinkedHashMap will keep the keys in the order they are inserted
    //which is currently sorted on natural ordering
    Map<K,V> sortedMap = new LinkedHashMap<K,V>();
    for(K key: keys){
        sortedMap.put(key, map.get(key));
    }
  
    return sortedMap;
    
    // http://javarevisited.blogspot.co.uk/2012/12/how-to-sort-hashmap-java-by-key-and-value.html
}

这给了我以下结果

他看到的不是陷阱的一部分,而是一大堆疯狂的雪

{4=a, a, 6=of, fo, 8=ton, not, 10=snow, wons, 12=crazy, yzarc}

现在,我的问题是如何用字典文本文件交叉检查单词的反向(最好是逐个字符)以查看反向单词是否构成有意义的单词?

我环顾四周,看到了一些建议,但找不到能帮助我了解如何解决当前问题的建议。

我曾想过使用二叉搜索树,我的想法是将字典文件加载到二叉树中并搜索树以查看反向单词是否构成现有单词,然后将它们打印出来。但是由于我不明白如何从一个文本文件中获取字符串然后将其与第二个文本文件进行比较,因此我无法继续进行。

最后,请您帮我指出正确的方向,让单词出现在二维数组中,好吗?!我尝试了几次,但它不起作用而且我没有想法:(!

提前致谢。

【问题讨论】:

  • 我不明白你输出的这一部分:“10=snow, wons, 12=crazy, yzarc”
  • 另外,你如何处理回文?
  • @fge 是输入文本文件中单词相同但相反的单词的结果。我没想到……:哦!谢谢,我会尝试写一个处理回文的方法。

标签: java string compare fileinputstream


【解决方案1】:

您可以逐行阅读字典文件。假设每行包含一个单词,您需要将该行放入哈希集中。然后,您可以检查从第一个文件中读取的单词,将每个单词反转并检查反转的单词是否在该哈希集中。

public Set<String> readFileIntoSet(String fileName) {
  Set<String> result = new HashSet<String>();
  for (Scanner sc = new Scanner(new File(fileName)); sc.hasNext(); ) 
    result.add(sc.nextLine());
  return result;
}

在您的 main 方法中添加对 readFileIntoSet 的调用:

Set<String> dictionary = readFileIntoSet("your_dictionary_file");

然后,找到反转的单词后,检查它是否出现在字典中:

if (dictionary.contains(reverseWord))
   system.out.println("this word: " + reverseWord + " appears in the dictionary!");

还请注意,String 类提供了“反向”方法。因此,您可以摆脱for(int i = wordLength-1; i &gt;= 0; i--){ ... } 循环而只使用reverseWord = foundWord.reverse();

【讨论】:

  • 非常感谢。你介意帮助我了解HashSet 在这里的工作原理吗,从上面的代码来看,它似乎只是在程序运行时充当一个临时口袋,用来存放字典文件,对吗?!
  • 是的。 HashSet 是一种数据结构,可以快速回答以下类型的问题:“数据结构维护的元素集合是否包含 X?”。您可以通过 .add() 方法将元素添加到集合中。您通过 .contains() 方法查询元素是否存在。
  • 您能帮我指出正确的方向吗?如何正确打印从example.txt 读取的单词及其反转单词?正如您在上面看到的那样,m 代码只允许打印出一些单词……有没有办法告诉 HashMap 打印出 example.txt 中的所有内容并将它们的反转?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
相关资源
最近更新 更多