0

名前のリストを印刷する必要があるときに、ブール値を使用して重複を検索したいと考えています。そのため、テキスト ファイル内の名前を読み取り、それをコンソールに出力するプログラムを作成する必要があります。ただし、この場合、コンパイラは機能しません。どうしてか分かりません?皆さん、私を助けてくれますか?

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]);

  }

}
4

1 に答える 1

1

あなたのコードは次のとおりです。

if(nNames<name.length && check = false)

check= false、 に代入falsecheckます。と比較checkするには、またはfalseを使用できます 。check==false!check

検証しようとしているものに応じて。以下のコードは、コンパイル エラーを取り除きます。

check == false //checks if check is false

または、

if(nNames<name.length && (check = false))
// above is same as if(nNames<name.length && false) // which will always be false
于 2013-05-14T15:46:18.277 に答える