以下の長いコードで申し訳ありませんが、ユーザーがオプション「5」を入力すると、私のコードは実行され、正しく実行されます。ユーザー入力からすべての値を既に取得しており、エラーはありませんが、ファイルに書き込まれませんでした。
私のファイルには現在データがあります:
Anthony Ducan;anthony;a123;55 Peter Street;3321444;VISA;3213504011223
Barry Blake;barry;a999;456 George Street;23239876;VISA;435677779876
Claire Rerg;clare;c678;925 Edward Lane;67893344;MASTERCARD;223344556677
テキストファイルとまったく同じ方法で保存しようとしています。void writeLinesToFile()
メソッドでのコーディングに何か問題がありますか、void newCust()
それともメソッドですか?
エラーがないことにまだ困惑していますが、ファイルに書き込んでいません。
public MainPage1(){ //start of MainPage1()
System.out.println("=====================================================");
System.out.println("Kreg Hotel Booking System - Main Page");
System.out.println("=====================================================");
System.out.println("[1] General Information");
System.out.println("[2] Make Booking");
System.out.println("[3] Active Booking Summary");
System.out.println("[4] Existing Customer");
System.out.println("[5] New Customer");
System.out.println("[6] Check Booking Status");
System.out.println("[7] Promotions");
System.out.println("[8] Exit\n");
System.out.println("Note: You have to select option 2 & 3 before option 4 & 5\n");
System.out.println("Please enter your selection: ");
try{choice = input.nextInt();}//to try to see if user input integer
catch (java.util.InputMismatchException e){//to catch if user didnt input integer
System.out.println("Invalid Input");
return;
}
while(true){ //start of do-while loop
if(choice==4||choice==5)
{
if(roomInfo.isEmpty()==true || addOns.isEmpty()== true){
System.out.println("Please enter option 2 or 3 before option 4 or 5");
choice=input.nextInt();
}
else if(choice == 4){existingCustomers();}
else if(choice ==5){newCust();}
else new MainPage1();
}
switch(choice){
case 1:break;
case 2:break;
case 3:break;
case 4:break;
case 5:newCust();
break;
case 6:break;
case 7:break;
case 8:System.out.println("Thank you. See you again soon");//exit the menu
System.exit(0);break;
default:System.out.println("Please enter number between 1 to 8");//prompts the user if they didnt enter between 1 to 8
choice = input.nextInt();
break;
}
}//while(choice!=1 || choice !=2 || choice!=3 ||choice!=4); // end of do-while loop
}//end of MainPage1()
public static void main(String[] args){//start of main page
new MainPage1();//to call the constructor
}//end of mainpage
public void writeLinesToFile(String filename,String[] linesToWrite,boolean appendToFile){
PrintWriter pw = null;
try {
if (appendToFile) {
//If the file already exists, start writing at the end of it.
pw = new PrintWriter(new FileWriter(filename, true));
}
else {
pw = new PrintWriter(new FileWriter(filename));
//this is equal to:
//pw = new PrintWriter(new FileWriter(filename, false));
}
for (int i = 0; i < linesToWrite.length; i++) {
pw.println(linesToWrite[i]+";");
}
pw.flush();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
//Close the PrintWriter
if (pw != null)
pw.close();
}
}
public void newCust(){
System.out.println("Welcome to Kreg Hotel Booking System");
System.out.println("==============================================");
System.out.println("Please enter your name: ");
n = input.nextLine();
n = input.nextLine();
System.out.println("Please enter your login username: ");
un = input.nextLine();
System.out.println("Please enter your login password: ");
pw = input.nextLine();
System.out.println("Please enter your address: ");
a = input.nextLine();
System.out.println("Please enter your contact: ");
con = input.nextLine();
System.out.println("Please enter your credit card type: ");
ct = input.nextLine();
System.out.println("Please enter your credit card number: ");
cn = input.nextLine();
MainPage1 util = new MainPage1();
util.writeLinesToFile("customerinfo.txt",new String[]{n,un,pw,a,con,ct,cn},true);
new MainPage1();
}
}//end of class