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;
}
}
は「e.printStackTrace() '' readInCustomer'のcatchブロックで、その後、あなたの質問にその痕跡を入れない場合は、問題を見つけるでしょう希望を入れてみてください – havexz
あなたのコードは完璧に動作します。適切なファイルパスがあり、正しく入力していることを確認してください。 –
私が問題になっていたのは、ロードしようとしていた2番目のtxtファイルに長い文字列が入っていたので、私は個人的な静的なCustomer [] customerList = new Customer [10]を増やす必要がありました。プライベート静的顧客に[]顧客リスト=新しい顧客[30]; – user1091869