2011-12-11 21 views
0

好的所以下面的代碼是編譯的,但是我有一個邏輯錯誤。好吧,等我輸入文件後,我希望它讀取的數據,我選擇在屏幕上打印數據我有加載不同的文件的選擇。但是,當我輸入我想要加載的新文件的名稱時,它不會加載該文件以及我爲其輸出的特定情況指定的錯誤消息。我想我必須在每次寫入之後刷新流緩衝區或類似的東西。所以,如果任何人都可以指出爲什麼會發生這種情況,那麼我會很感激。如何在java中重新載入一個不同的文件在打印完一個之前的文件後

import java.util.Scanner; import java.io. *;

public class Driver { 
    private static int numberOfCustomer = 0; 
    private static Customer[] customerList = new Customer[10]; 

     private static void readInCustomer(String file){ 
     FileReader freader; 
     BufferedReader inputFile; 
     try{ 
      freader = new FileReader(file); 
      inputFile = new BufferedReader(freader); 
      String strLine; 


      while ((strLine = inputFile.readLine()) != null) { 
        customerList[numberOfCustomer] = new Customer(); 
        customerList[numberOfCustomer].ID = strLine; 
        customerList[numberOfCustomer].name =     inputFile.readLine(); 
        customerList[numberOfCustomer].address = inputFile.readLine(); 
        customerList[numberOfCustomer].phone = inputFile.readLine(); 
        numberOfCustomer++; 
      } 

      inputFile.close(); 
     }catch(Exception e){ 
      System.out.println("Could not find file "+file+" System will now exit"); 
      System.exit(1); 

     } 

     return; 
    } 

     private static void printCustomer(Customer customer){ 
     System.out.println("The Customer Data corresponding to Customer Number " + customer.ID + " is:"); 
     System.out.println("Name:\t\t\t"+customer.name); 
     System.out.println("Address:\t\t"+customer.address); 
     System.out.println("Telephone:\t\t"+customer.phone); 
     System.out.println(); 
     return; 
    } 

     private static void printAll(){ 
     boolean hasID = false; 
      Scanner keyboard = new Scanner(System.in); 

     System.out.println("All customers from data file "+numberOfCustomer); 


     System.out.println(" Here they are!!! "); 
     for(int i=0; i<numberOfCustomer; i++){ 
      if(customerList[i] != null){ 
       System.out.println("The Customer Data corresponding to Customer Number " + customerList[i].ID + " is:"); 
       System.out.println("Name:\t\t\t"+customerList[i].name); 
       System.out.println("Address:\t\t"+customerList[i].address); 
       System.out.println("Telephone:\t\t"+customerList[i].phone); 
      } 
     } 
     if(!hasID){ 

     System.out.println(""); 
     } 
     System.out.println("Would you like to go to the menu? (Y or N):"); 
     String input = keyboard.nextLine(); 
     char repeat = input.charAt(0); 
     if(repeat == 'Y' || repeat == 'y'){Menu();} 
        return; 
    } 

    private static void Menu(){ 
     boolean hasID = false; 
     Scanner keyboard = new Scanner(System.in); 

     System.out.println("YOU MAY CHOOSE FROM THE FOLLOWING OPTIONS:"); 
     System.out.println("A. SEARCH for a customer by ID number"); 
     System.out.println("B. DISPLAY the entire Customer List"); 
     System.out.println("C. RE-LOAD DATA from a different data file"); 
     System.out.println("D. QUIT:"); 

     String choice = keyboard.nextLine(); 

     char repeat = choice.charAt(0); 
     if(repeat == 'A' || repeat == 'a'){Scostomer();} 
      if(repeat == 'B' || repeat == 'b'){printAll();} 
      if(repeat == 'C' || repeat == 'c'){mainn();} 
       return; 

     } 

    public static void Scostomer(){ 
    boolean hasID = false; 
     Scanner keyboard = new Scanner(System.in); 

     System.out.println("Type in the Id you are search for"); 

     String customerID = keyboard.nextLine(); 

     for(int i=0; i<numberOfCustomer; i++){ 
      if((customerList[i]!=null) && (customerID.equals(customerList[i].ID))){ 
       hasID = true; 
       printCustomer(customerList[i]); 
       i=customerList.length; 
      } 
     } 

     if(!hasID){ 
      System.out.println("Sorry, customer not found."); 
     } 
     System.out.println("Would you like to search for another custnomer? (Y or N):"); 
     String input = keyboard.nextLine(); 
     char repeat = input.charAt(0); 
     if(repeat == 'Y' || repeat == 'y'){Scostomer();} 
      if(repeat == 'N' || repeat == 'n'){Menu();} 
        return; 
    } 

    public static void main(String arg[]){ 
     Scanner keyboard = new Scanner(System.in); 
      System.out.println("Enter the fileName that contains the data of your customers: "); 
     readInCustomer(keyboard.nextLine()); 
     Menu(); 
     return; 
     } 

    public static void mainn(){ 
     Scanner keyboard = new Scanner(System.in); 

      System.out.println("Enter the fileName that contains the data of your customers: "); 
     readInCustomer(keyboard.nextLine()); 
     Menu(); 
     return; 

    } 
} 
+0

嘗試把「e.printStackTrace()'在readInCustomer'的'catch塊,然後希望你會發現這個問題如果沒有的話把那個跟蹤你的問題? – havexz

+0

你的代碼對我來說非常適合。檢查以確保您具有正確的文件路徑,並且正確輸入。 –

+0

我的問題是,我想加載的第二個txt文件有一個更長的字符串在所以我不得不增加私人靜態Customer [] customerList = new Customer [10];到私有靜態Customer [] customerList = new Customer [30]; – user1091869

回答

0

適用於我(加載文件,鍵入c加載新文件,鍵入b顯示全部)。我建議有一些簡單的錯誤(錯誤的文件名或一些愚蠢的東西),所以你需要記錄異常細節,而不是在你的catch塊中忽略它們。

} catch(Exception e) { 
     e.printStackTrace(); // should tell you what's wrong! 
     System.out.println("Could not find file "+file+" System will now exit"); 
     System.exit(1); 

    } 

法拉盛是不是與此有關,你是閱讀,不寫。你也許可以整理整個菜單/輸入循環,這有點古怪(但它的工作原理!)。例如,你的主要的mainn方法是相同的 - 你有沒有考慮過抽象出一個單獨的方法?

您可以使用List來代替10個客戶,而不用擔心要讀取多少客戶(我只更改了您的代碼以使用List而不是數組) :

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class Driver { 
    private static int numberOfCustomer = 0; 
    // List instead of array! 
    private static List<Customer> customerList = new ArrayList<Customer>(); 

    private static void readInCustomer(String file) { 
     FileReader freader; 
     BufferedReader inputFile; 
     try { 
      freader = new FileReader(file); 
      inputFile = new BufferedReader(freader); 
      String strLine; 

      while ((strLine = inputFile.readLine()) != null) { 
       Customer customer = new Customer(); 
       customer.ID = strLine; 
       customer.name = inputFile.readLine(); 
       customer.address = inputFile.readLine(); 
       customer.phone = inputFile.readLine(); 
       customerList.add(customer); // add to the List! 
      } 

      inputFile.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("Could not find file " + file 
        + " System will now exit"); 
      System.exit(1); 

     } 

     return; 
    } 

    private static void printCustomer(Customer customer) { 
     System.out 
       .println("The Customer Data corresponding to Customer Number " 
         + customer.ID + " is:"); 
     System.out.println("Name:\t\t\t" + customer.name); 
     System.out.println("Address:\t\t" + customer.address); 
     System.out.println("Telephone:\t\t" + customer.phone); 
     System.out.println(); 
     return; 
    } 

    private static void printAll() { 
     boolean hasID = false; 
     Scanner keyboard = new Scanner(System.in); 

     System.out.println("All customers from data file " + numberOfCustomer); 

     System.out.println(" Here they are!!! "); 
     for (Customer customer : customerList) { 
      System.out 
        .println("The Customer Data corresponding to Customer Number " 
          + customer.ID + " is:"); 
      System.out.println("Name:\t\t\t" + customer.name); 
      System.out.println("Address:\t\t" + customer.address); 
      System.out.println("Telephone:\t\t" + customer.phone); 
     } 

     if (!hasID) { 
      System.out.println(""); 
     } 
     System.out.println("Would you like to go to the menu? (Y or N):"); 
     String input = keyboard.nextLine(); 
     char repeat = input.charAt(0); 
     if (repeat == 'Y' || repeat == 'y') { 
      Menu(); 
     } 
     return; 
    } 

    private static void Menu() { 
     boolean hasID = false; 
     Scanner keyboard = new Scanner(System.in); 

     System.out.println("YOU MAY CHOOSE FROM THE FOLLOWING OPTIONS:"); 
     System.out.println("A. SEARCH for a customer by ID number"); 
     System.out.println("B. DISPLAY the entire Customer List"); 
     System.out.println("C. RE-LOAD DATA from a different data file"); 
     System.out.println("D. QUIT:"); 

     String choice = keyboard.nextLine(); 

     char repeat = choice.charAt(0); 
     if (repeat == 'A' || repeat == 'a') { 
      Scostomer(); 
     } 
     if (repeat == 'B' || repeat == 'b') { 
      printAll(); 
     } 
     if (repeat == 'C' || repeat == 'c') { 
      mainn(); 
     } 
     return; 

    } 

    public static void Scostomer() { 
     boolean hasID = false; 
     Scanner keyboard = new Scanner(System.in); 

     System.out.println("Type in the Id you are search for"); 

     String customerID = keyboard.nextLine(); 

     // iterate over the List! 
     for (Customer customer : customerList) { 
      if (customerID.equals(customer.ID)) { 
       hasID = true; 
       printCustomer(customer); 
       break; 
      } 
     } 

     if (!hasID) { 
      System.out.println("Sorry, customer not found."); 
     } 
     System.out 
       .println("Would you like to search for another custnomer? (Y or N):"); 
     String input = keyboard.nextLine(); 
     char repeat = input.charAt(0); 
     if (repeat == 'Y' || repeat == 'y') { 
      Scostomer(); 
     } 
     if (repeat == 'N' || repeat == 'n') { 
      Menu(); 
     } 
     return; 
    } 

    public static void main(String arg[]) { 
     Scanner keyboard = new Scanner(System.in); 
     System.out 
       .println("Enter the fileName that contains the data of your customers: "); 
     readInCustomer(keyboard.nextLine()); 
     Menu(); 
     return; 
    } 

    public static void mainn() { 
     Scanner keyboard = new Scanner(System.in); 

     System.out 
       .println("Enter the fileName that contains the data of your customers: "); 
     readInCustomer(keyboard.nextLine()); 
     Menu(); 
     return; 

    } 
} 
+0

謝謝你!我見過e.printStackTrace();之前好它它給了我這個錯誤java.lang.ArrayIndexOutOfBoundsException:10 \t在Driver.readInCustomer(Driver.java:33),所以我只是inscreaase數從10 - 30,它的工作 – user1091869

+0

你應該考慮使用一個集合(即列表),而不是數組,如果你不能保證你總是會有最多10個客戶。 –

+0

但是請注意,打印到System.out(以及使用e.printStackTrace())通常會被忽略 - 建議使用日誌框架,例如commmons-logging,slf4j等。但是對於快速和骯髒的日誌記錄或獨立的命令行應用程序來說,這很好。 –

相關問題