java - Use boolean to search duplicate -
i want use boolean search duplicate when need print out list of names. need write program read names in text file , print out console. compiler doesn't work in case. don't know why? can guys me?
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: program sort names stored in file."); system.out.println(); //get input string filename; system.out.print("enter 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 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]); } }
your code :
if(nnames<name.length && check = false)
check= false
, assigns false
check
. compare check
false
can use check==false
or !check
.
depending on trying validate. below code remove compilation error:
check == false //checks if check false
or,
if(nnames<name.length && (check = false)) // above same if(nnames<name.length && false) // false
Comments
Post a Comment