好的所以下面的代碼是編譯的,但是我有一個邏輯錯誤。好吧,等我輸入文件後,我希望它讀取的數據,我選擇在屏幕上打印數據我有加載不同的文件的選擇。但是,當我輸入我想要加載的新文件的名稱時,它不會加載該文件以及我爲其輸出的特定情況指定的錯誤消息。我想我必須在每次寫入之後刷新流緩衝區或類似的東西。所以,如果任何人都可以指出爲什麼會發生這種情況,那麼我會很感激。如何在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;
}
}
嘗試把「e.printStackTrace()'在readInCustomer'的'catch塊,然後希望你會發現這個問題如果沒有的話把那個跟蹤你的問題? – havexz
你的代碼對我來說非常適合。檢查以確保您具有正確的文件路徑,並且正確輸入。 –
我的問題是,我想加載的第二個txt文件有一個更長的字符串在所以我不得不增加私人靜態Customer [] customerList = new Customer [10];到私有靜態Customer [] customerList = new Customer [30]; – user1091869