我想要使用布爾值來搜索重複的時候,我需要打印出一個名稱列表。所以我需要編寫一個程序來讀取文本文件中的名稱並將其打印出來以便控制檯輸出。但編譯器在這種情況下不起作用。我不知道爲什麼?你們能幫我嗎?使用布爾值來搜索重複
import java.io.*;
import java.util.*;
public class NameSorter
{
public static void main(String[] args) throws Exception
{
BufferedReader cin, fin;
cin = new BufferedReader(new InputStreamReader(System.in));
//Description
System.out.println("Programmer: Minh Nguyen");
System.out.println("Description: This program is to sort names stored in a file.");
System.out.println();
//Get input
String fileName;
System.out.print("Enter the file's name: ");
fileName = cin.readLine();
fin = new BufferedReader(new FileReader(fileName));
int nNames = 0;
String[] name = new String[8];
//initialize array elements
for(int i=0; i<name.length;i++)
{
name[i]=" ";
}
// read text file
while(fin.ready())
{
String aName = fin.readLine();
String temp = aName;
boolean check;
if(temp.compareTo(" ")>0)
{
for(int i=0; i<name.length;i++)
{
if(temp.compareToIgnoreCase(name[i])==0)
{
check = true;
break;
}
}
}
if(nNames<name.length&& check = false)
{
name[nNames++] = temp;
}
}
}
fin.close();
// Sort the names aphabetically.
for(int i=0;i<nNames; i++)
{
int j;
for(j=i+1;j<nNames; j++)
{
if(name[i].compareToIgnoreCase(name[j])>0)
{
String temp = name[i];
name[i] = name[j];
name[j] = temp;
}
}
}
for(int i=0; i<name.length;i++)
System.out.println(name[i]);
}
}
這將有助於如果你包括你看到的錯誤消息。 「它不起作用」只是以你不喜歡的方式標記你。另外,嘗試將'check = false'更改爲'check == false'。 – 2013-05-14 15:46:21