我很抱歉抱歉这长条代码!我的目的是在词典“wb array”中找到我诗中的匹配单词,但它会找到任何东西(返回结果没有匹配芽我知道有匹配的单词)我强烈怀疑它的iether我的嵌套循环但也想知道它是否没有成功导入你可以给我提示可能是什么问题吗?谢谢你先进的
int i = 0;
String[] wb = new String[1];
String[] poem = new String[1];
Scanner input = new Scanner( System.in);
boolean flag = false;
// Try to Import Dictionary
try{
BufferedReader br = new BufferedReader(new FileReader("C:/poem/web2.txt"));
String line;
int counter = 0;
while ((line = br.readLine()) != null)
{
if (wb.length == counter)
{
//expand list
wb = Arrays.copyOf(wb, wb.length + 1);
}
wb[counter] = line;
counter++;
}
br.close();
}catch(Exception e)
{
e.printStackTrace();
}
try{
BufferedReader brp = new BufferedReader(new FileReader("C:/poem/poem.txt"));
String line;
int counter = 0;
String[] temp;
while ((line = brp.readLine()) != null)
{
temp = line.split("\\s+");
for ( int icount = 0; icount < temp.length; icount++)
{
if (poem.length == counter)
{
// expand list
poem = Arrays.copyOf(poem, poem.length + 1);
}
poem[counter] = temp[icount];
counter++;
}
}
brp.close();
}catch(Exception e)
{
e.printStackTrace();
}
// check for matching words in poem and Dictionary
for ( int counterpoem = 0; counterpoem < poem.length; counterpoem++)
{
flag = false;
for ( int counterwb = 0; counterwb < wb.length; counterwb++)
{
if ( wb[counterwb] == poem[counterpoem])
{
flag = true;
}
}
if ( flag == true )
{
System.out.println(poem[counterpoem] + " Not unique");
}
else
System.out.println(poem[counterpoem] + " unique");
}