所以我給了一組電子郵件,我應該閱讀它們,將它們存儲在一個數組中,刪除重複項並打印「剩菜」。我幾乎可以做到這一點,但刪除重複項後,當我打印剩菜時,它會打印額外的null
。Java字符串循環陣列空
這是我的代碼。有人能指出我的修復方向嗎?
public class Duplicate {
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = keyboard.nextLine();
if(fileName.equals("")){
System.out.println("Error: User did not specify a file name.");
}
else{Scanner inputStream = null;
try{inputStream = new Scanner(new File(fileName));
}
catch(FileNotFoundException e){
System.out.println("Error: "+ fileName + " does not exist.");
System.exit(0);
}
String [] address = new String[100];
for(int i=0;inputStream.hasNextLine();i++){
String email = inputStream.nextLine();
address[i]=email.toLowerCase();
//System.out.println(address[i]);
}
Set<String> mail = new HashSet<String>(Arrays.asList(address));
for(String email:mail){
System.out.println(email);
}
像早先的問題一樣,這聽起來像是作業。請務必使用'作業'標籤。 – Leigh 2012-04-08 02:48:30