Java 如何使用Hashmap对字典中的混乱单词进行排序
我无法让我的程序编译。我要做的是让程序打印出所有乱七八糟的单词,并在旁边打印出字典中的单词。我相信这是我嵌套循环的方式中的一个错误,但我无法理解。有人能帮我吗Java 如何使用Hashmap对字典中的混乱单词进行排序,java,hashmap,Java,Hashmap,我无法让我的程序编译。我要做的是让程序打印出所有乱七八糟的单词,并在旁边打印出字典中的单词。我相信这是我嵌套循环的方式中的一个错误,但我无法理解。有人能帮我吗 public static void main(String[] args) throws Exception { if (args.length < 2) die("Must give name of two input files on cmd line."); BufferedReader
public static void main(String[] args) throws Exception
{
if (args.length < 2) die("Must give name of two input files on cmd line.");
BufferedReader dictionaryFile = new BufferedReader( new FileReader( args[0] ));
BufferedReader jumbleFile = new BufferedReader( new FileReader(args[0] ));
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
ArrayList<String> jumbleWords = new ArrayList<String>();
ArrayList<String> dictionaryWords = new ArrayList<String>();
ArrayList<String> keysList = new ArrayList<String>();
while(jumbleFile.ready())
{
String jWord=jumbleFile.readLine();
jumbleWords.add(jWord);
}
jumbleFile.close();
Collections.sort(jumbleWords);
while(dictionaryFile.ready())
{
String dWord= dictionaryFile.readLine();
String dictWord= toCanonical(dWord);
if(map.containsKey(dictWord))
{
map.get(dictWord);
map.put(dWord, map.get(dictWord));
}
else
{
ArrayList<String> dictionaryWords2 = new ArrayList<String>();
dictionaryWords2.add(dWord);
map.put(dictWord, dictionaryWords2);
}
for( String i : map.keySet())
{
keysList.add(i);
}
Collections.sort(keysList);
for (String key : keysList)
{
System.out.print(key);
String toCanJWord= toCanonical(key);
if(map.containsKey(toCanJWord))
{
map.get(toCanJWord);
Collections.sort(map.get(toCanJWord));
for(map.get(toCanJWord))
{
System.out.print(toCanJWord);
}
}
System.out.println();
}
private static String toCanonical( String word )
{
char[] letters = word.toCharArray();
Arrays.sort(letters);
return new String(letters);
}
private static void die( String errmsg )
{
System.out.println( "\nFATAL ERROR: " + errmsg + "\n" );
System.exit(0);
}
}`
publicstaticvoidmain(字符串[]args)引发异常
{
if(args.length<2)die(“必须在cmd行上给出两个输入文件的名称”);
BufferedReader dictionaryFile=新的BufferedReader(新的文件读取器(args[0]);
BufferedReader jumbleFile=新的BufferedReader(新文件读取器(args[0]);
HashMap=newHashMap();
ArrayList jumbleWords=新的ArrayList();
ArrayList DictionaryWord=新的ArrayList();
ArrayList keysList=新建ArrayList();
while(jumbleFile.ready())
{
字符串jWord=jumbleFile.readLine();
添加(jWord);
}
jumbleFile.close();
Collections.sort(jumbleWords);
while(dictionaryFile.ready())
{
字符串dWord=dictionaryFile.readLine();
字符串dictWord=toCanonical(dWord);
if(地图容器(dictWord))
{
map.get(dictWord);
map.put(dWord,map.get(dictWord));
}
其他的
{
ArrayList dictionaryWords2=新的ArrayList();
字典words2.add(dWord);
map.put(dictWord、dictionaryWords2);
}
for(字符串i:map.keySet())
{
添加(i);
}
Collections.sort(keysList);
for(字符串键:keysList)
{
系统输出打印(键);
字符串tocanjord=toCanonical(键);
if(地图CONTANSKEY(TOCANWORD))
{
map.get(tocanjord);
Collections.sort(map.get(toCanJWord));
for(map.get(tocanjord))
{
系统输出打印(TOCANWORD);
}
}
System.out.println();
}
私有静态字符串到规范(字符串字)
{
char[]字母=word.toCharArray();
数组。排序(字母);
返回新字符串(字母);
}
专用静态无效模具(字符串errmsg)
{
System.out.println(“\n致命错误:+errmsg+”\n”);
系统出口(0);
}
}`
您有几个问题。首先,您在for循环的末尾缺少一个
,如下所示:
for (String key : keysList)
{
System.out.print(key);
String toCanJWord = toCanonical(key);
if(map.containsKey(toCanJWord))
{
map.get(toCanJWord);
Collections.sort(map.get(toCanJWord));
//this isn't correct. Not sure what you are trying to do here
//but this is why it won't compile
for(map.get(toCanJWord))
{
System.out.print(toCanJWord);
}
}
}//missing this closing bracket
您的for循环也存在问题,请参阅注释 您的for循环错误:
for(map.get(toCanJWord))
{
System.out.print(toCanJWord);
}
它需要采用以下格式:
for(String wordToPrint : map.get(toCanJWord))
{
System.out.print(wordToPrint );
}
您的代码包含大量bug,但导致进一步处理几乎立即停止的bug是(map.get(toCanJWord))
。一旦你解决了这个问题,我建议你停下来重新考虑你的方法<代码>字符串是不可变的-使用数组。在其“backing array”(如tocanonic
)上的sort(char[])
会给您带来各种痛苦。它现在可以编译了,但它只是一个无限的lettersHow输出。如果我不知道tocanjord所在的数组,我该对它进行排序吗?”Collections.sort(map.get(toCanJWord))'不起作用?假设您希望映射包含: