2016-12-04 102 views
1

我必須設計成用作地址簿中的程序中的特定行。它有3種方法:一種允許添加新聯繫人,一種可以打印出圖書中的所有聯繫人,另一種允許您按姓氏搜索聯繫人,並只打印聯繫人信息。我有前兩個完美的工作,但最後一個總是打印列表中的所有聯繫人,我不知道爲什麼。搜索和打印使用Java IO

問題出在search()方法中。我添加了很多評論,這樣你們就可以告訴程序中發生了什麼。

警告:代碼傳入的牆。

/** 
*/ 
import java.util.*; 
import java.io.*; 

public class ContactList 
{ 
    /** 
    Contact list file name 
    */ 
    private String filename; 

    /** 
    ContactList constructor accepts a String parameter 
    */ 
    public ContactList(String inFileName) 
    { 
     filename = inFileName; 
    } 

    /** 
     3) add a new record to the file. Open the file for writing in append mode. 
     a) prompt the user to enter data for each field in the record. Each field is a String. 
      The last name is required. If the last name is the empty string(""), return to the menu. 
     b) when the user has completed entering data(i.e., all the fields have been prompted), re-display the user choices 
     c) do not overwrite existing data 
    */ 
    public void new_record() 
    {  
     /* 
     Prompt for data: 
     Last name 
     First name 
     Phone 

    */ 
     //Craete a scanner object 
     Scanner in = new Scanner(System.in); 


     //prompt for the last name 
     System.out.println("Please Enter Contact's Last Name: "); 

     //input the last name 
     String lastName = in.nextLine(); 

     //the Last_name must not be empty 
     if(lastName.length() > 0) 
     { 
      //get the first name and the phone 
      System.out.println("Please Enter Contact's First Name: "); 
      String firstName = in.nextLine(); 

      System.out.println("Please Enter Contact's Phone Number [xxx-xxx-xxxx]: "); 
      String phone = in.nextLine(); 

      //create the output string 
      String contact = lastName + ","; 
      contact = contact + firstName + ","; 
      contact = contact + phone; 

      System.out.println(""); 
      System.out.println("Last Name: " + lastName); 
      System.out.println("First Name: " + firstName); 
      System.out.println("Phone: " + phone); 
      System.out.println("");   

      //try to open the file for writing - append the data 
      FileWriter fw = null; 
      BufferedWriter bw = null;   
      try 
      {    
       fw = new FileWriter(filename,true); 
       bw = new BufferedWriter(fw); 
      } 
      catch(IOException ioe) 
      { 
       System.out.println("new_record: Exception opening the file for writing"); 
      } 
      //try to wrtie the data 
      try 
      { 
       bw.write(contact); 
       bw.newLine();  
      } 
      catch(IOException ioe) 
      { 
       System.out.println("new_record: Exception writing to the file"); 
      } 
      //try to close the file 
      try 
      { 
       bw.flush(); 
       bw.close(); 
       fw.close(); 
      } 
      catch(IOException ioe) 
      { 
       System.out.println("new_record: Exception closing the file"); 
      } 

     }//end of test of Last_name 

    }//end of new_record 

    /** 
    2) display all last names and first names in the file. 
     Open the file for reading, read each record and 
     display the field values. 
     a) display all the lastName, firstName paired fields in the file; 
     display with the format lastName, firstName 
     b) when all records have been displayed, display the record count - the record count is the number of records read and should equal the number of records in the file 
     c) after all the records and the count have been displayed, display the user choices 

    */ 
    public void display_names() 
    { 
     FileReader fn = null; 
     BufferedReader br = null; 

     //try to open the file for reading 
     try 
     { 
      fn = new FileReader(filename); 
      br = new BufferedReader(fn); 
     } 
     catch(IOException ioe) 
     { 
      System.out.println("display_names: Exception opening the file"); 
     } 

     /* 
     try to read each record and display the field values. 
     a) display all the lastName, firstName paired fields in the file; 
      display with the format lastName, firstName 
      count each record that is read 
     */ 
     int counter = 0; //record counter 
     try 
     { 
      //read the first record 
      String line = br.readLine(); 
      //while the record is not null, display the record, count the record 
      while(line != null) 
      { 
       System.out.println(line); 
       counter++; 
       line = br.readLine(); 
      }  
     } 
     catch(IOException ioe) 
     { 
      System.out.println("display_names: Exception reading the file"); 
     } 

     //try to close the file 
     try 
     { 
      br.close(); 
      fn.close();  
     } 
     catch(IOException ioe) 
     { 
      System.out.println("display_names: Exception closing the file"); 
     } 
     //display a count of the records read 
     System.out.println(""); 
     System.out.println("Records Displayed: " + counter); 
     System.out.println(""); 
    }//end of display_names 

    /** 
     1) search an address file for a particular last name 
     and then display the Last name, the first name, and 
     the phone for each match 
     2) display the count of records which match the last name 

    */ 
    public void search(String findMe) 
    { 
     FileReader fn = null; 
     BufferedReader br = null; 

     //try to open the file for reading 
     try 
     { 
      fn = new FileReader(filename); 
      br = new BufferedReader(fn); 
     } 
     catch(IOException ioe) 
     { 
      System.out.println("search: Exception opening the file"); 
     } 

     //try to read each record 
     int counter = 0; 
     try 
     { 
      String line = br.readLine(); 
      while(line != null) 
      { 
       String [] fields = line.split(","); 
       if(findMe.equals(fields[0])); 
       { 
        System.out.println(line); 
        System.out.println(""); 
        counter++; 
       } 
       line = br.readLine(); 
      }  
     } 
     catch(IOException ioe) 
     { 
      System.out.println("Search: Exception Reading the File."); 
     } 

     //Try to close the file 
     try 
     { 
      br.close(); 
      fn.close();  
     }     
     catch(IOException ioe) 
     { 
      System.out.println("search: Exception closing the file"); 
     } 
     //dislay a count of the records found 
     System.out.println("Records Searched: " + counter); 
     System.out.println("");  
    }//end of search 
}//end of class 

這裏是非常簡單的測試類,與它去:

import java.util.*; 
public class TestContactList 
{ 
    public static void main(String [] args) 
    { 
     final int ONE = 1; 
     final int TWO = 2; 
     final int THREE = 3; 
     final int FOUR = 4; 
     final int FIVE = 5; 

     Scanner scan = new Scanner(System.in); 
     while(true) 
     { 
      System.out.println("1) Search the file for a last name "); 
      System.out.println("2) Display all last & first names in file"); 
      System.out.println("3) Add a new record to the file "); 
      System.out.println("4) End the program "); 
      System.out.print("Please choose 1 - 4: "); 
      int choice = scan.nextInt(); 
      scan.nextLine(); 

      /* 
      Create a new ContactList object with the name of the 
      contact list file. 
      */ 
      ContactList cl = new ContactList("MyAddressBook.txt"); 

      if(choice == ONE) 
      { 
       System.out.print("Enter name to find: "); 
       String findMe = scan.nextLine(); 
       cl.search(findMe); 
      } 

      if(choice == TWO) 
      { 
       // then call display names 
       cl.display_names(); 
      } 

      if(choice == THREE) 
      { 
       // then call new record 
       cl.new_record(); 
      } 

      if(choice == FOUR) { System.exit(0); } 
     } 
    } 
} 
+0

我不知道爲什麼。我正在運行調試器,字段顯然不同,但名稱仍然打印。奇怪...你嘗試運行一個調試器嗎? –

+0

@Nick Ziebert是的,我做過了:/這實際上是我編寫我編程課程的作業,我今天花了2個小時與我的助教一起努力弄清楚什麼是錯誤的。在這一點上,搜索方法中的代碼與他的字面上完全相同,但由於某種原因,我的印刷版本只有他的印刷版本。 –

回答

2

我發現的錯誤。你必須拿走「;」

if(findMe.equals(fields[0])); <---oops 
+0

哦,我的天哪。 2個小時的兩個人看着這個東西,我們沒有注意到這一點。非常感謝! –

+0

這是我們所有人學到的教訓。哈。 –