[英]Why won't my print line work?
I want this program to sort jumbled words in alphabetical order, be compared to the dictionary, and be matched with dictionary words. 我希望该程序按字母顺序对混乱的单词进行排序,与字典进行比较,并与字典单词匹配。 I get it to print out the jumbles in alphabetical order but it will not print the dictionary words even though I have a print line.
我得到它以字母顺序打印出杂物,但是即使我有打印行,它也不会打印字典单词。
public class i
{
public static void main(String[] args) throws Exception
{
if (args.length < 2 ) die("No dictionary");
BufferedReader dictionaryFile = new BufferedReader( new FileReader( args[0] ));
BufferedReader jumbleFile = new BufferedReader( new FileReader( args[1] ));
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
ArrayList<String> jumbleWords = new ArrayList<String>();
ArrayList<String> dictionaryWords = new ArrayList<String>();
while(dictionaryFile.ready())
{
String dictWord = dictionaryFile.readLine();
dictionaryWords.add(dictWord);
}
while(jumbleFile.ready())
{
String word = jumbleFile.readLine();
//String canWord = toCanonical(word);
jumbleWords.add(word);
map.put(word, new ArrayList<String>());
}
Collections.sort(jumbleWords);
for(String dictionaryWord : dictionaryWords)
{
String canDictWord = toCanonical(dictionaryWord);
if(map.containsValue(canDictWord))
{
ArrayList<String> listOfWords = map.get(canDictWord);
listOfWords.add(dictionaryWord);
}
}
ArrayList<String> keysList = new ArrayList(map.keySet());
Collections.sort(keysList);
for(String key : keysList)
{
System.out.print(key);
ArrayList<String> list = map.get(key);
Collections.sort(list);
for(String word : list)
{
if (toCanonical(key).equals(toCanonical(word)))
System.out.print(word + " ");
}
System.out.println();
}
}
private static void die( String errmsg )
{
System.out.println( "\nFATAL ERROR: " + errmsg + "\n" );
System.exit(0);
}
private static String toCanonical( String word )
{
char[] letters = word.toCharArray();
Arrays.sort(letters);
return new String(letters);
}
}
The values of your map are defined to be of type ArrayList<String>
, but in the code below, your trying to check whether it contains a regular String
. 映射的值被定义为
ArrayList<String>
类型,但是在下面的代码中,您尝试检查其是否包含常规String
。
if(map.containsValue(canDictWord)) {
// ...
}
This will always return false, so the reason your println
doesn't work is because the map's value ArrayList
s will always be empty. 这将始终返回false,因此您的
println
不起作用的原因是因为映射的值ArrayList
始终为空。
It's difficult to figure out what you're actually trying to achieve, and the formatting (or lack thereof) of your code makes it even harder. 很难弄清您实际要实现的目标,而代码的格式(或缺乏代码格式)则使其变得更加困难。 You probably should try making a map with the dictionary words as keys, and the jumbled words as a list of values.
您可能应该尝试以词典词作为键,将混杂词作为值列表来制作地图。 Then you can use
map.containsKey(toCanonical(jumbledWord))
and add the jumbled word to the list of values for that key. 然后,您可以使用
map.containsKey(toCanonical(jumbledWord))
并将混乱的单词添加到该键的值列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.