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;
}
}
시도가'readInCustomer''의 catch 블록에서 'e.printStackTrace()을 넣고 귀하의 질문에 그 흔적을 넣어하지 않을 경우 다음 문제를 찾을거야 희망? – havexz
코드가 완벽하게 작동합니다. 올바른 파일 경로와 올바르게 입력했는지 확인하십시오. –
제가 문제는 두 번째 txt 파일에로드하려는 길이가 길어서 개인 정적 Customer [] customerList = new Customer [10]을 늘려야한다는 것입니다. 개인 정적 고객에게 [] customerList = 새 고객 [30]; – user1091869