2011-12-11 7 views
0

Okこのコードはコンパイルしていますが、論理エラーがあります。私はそれを読んで、私は画面上のデータを印刷することを選択したいデータとファイルを入力した後、私は別のファイルを読み込むの選択肢があります。しかし、ロードしたい新しいファイルの名前を入力すると、その特定の状況に対して指定したファイルとエラーメッセージがロードされません。私はそれぞれの書き込み後にストリームバッファをフラッシュしなければならないと思います。だから誰かがなぜこれが起きているのかを指摘できるなら、私はそれを感謝するだろう。前のファイルを印刷した後、別のファイルを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

私が問題になっていたのは、ロードしようとしていた2番目のtxtファイルに長い文字列が入っていたので、私は個人的な静的なCustomer [] customerList = new Customer [10]を増やす必要がありました。プライベート静的顧客に[]顧客リスト=新しい顧客[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); 

    } 

あなたはは書いていない、を読んでいるようフラッシングは、ここでは関係ありません。おそらく全体のメニュー/入力ループも整理できますが、ちょっと変なことです(ただし動作​​します!)。たとえば、のメインのメインののメソッドは同じです - 別のメソッドに抽象化することを考えましたか?

自分自身を10人の顧客に限定する代わりに、リストを使用して、読み込む顧客の数を心配する必要はありません(配列の代わりにリストを使用するように変更しただけです) :?

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に与えました:Driver.readInCustomer(Driver.java:33)に10 \tがありますので、10から30までの数字をinscreaaseしています。 – user1091869

+0

Collection(つまりList )を使用すると、最大10人の顧客が常にいることを保証できない場合は、配列の代わりに –

+0

しかし、System.outへの印刷(そしてe.printStackTrace()を使用)は一般的に嫌われています。代わりにcommmons-logging、slf4jなどのロギングフレームワークが推奨されます。しかし、速くて厄介なロギングやスタンドアロンのコマンドラインアプリケーションでは問題ありません。 –

関連する問題