【发布时间】:2015-01-22 14:48:58
【问题描述】:
我似乎无法将要打印的内容打印到“transaction-list.txt”文件中。然而,它确实创建了文件,但在程序运行时它什么也不打印。它应该可以无缝工作,只是不知道为什么。
这是我的代码:
final String acc_name= "Edward";
final int acc_num=123456;
final int acc_password=456789;
boolean quit = false;
int i;
PrintWriter outFile = new PrintWriter("transaction-list.txt");
outFile.println("HELLO!");
//to output timestamp
String timestamp = new java.text.SimpleDateFormat("MM/dd/yyyy h:mm:ss").format(new Date());
//System.out.print(timestamp);
//ask user to key in the account number and password and stores it into acc_num and acc_password variables
String stringAcc_num = JOptionPane.showInputDialog("Please enter your account number: ");
int Acc_num = Integer.parseInt(stringAcc_num);
String stringAcc_password = JOptionPane.showInputDialog("Please enter your account password: ");
int Acc_password = Integer.parseInt(stringAcc_password);
while (Acc_num != acc_num || Acc_password != acc_password){
for (i = 1; i <= 2; i++) {
String error = "YOUR ACCOUNT NAME AND YOUR ACCOUNT PASSWORD IS NOT A MATCH!!";
JOptionPane.showMessageDialog(null, error, "ALERT!", JOptionPane.ERROR_MESSAGE);
stringAcc_num = JOptionPane.showInputDialog("Please enter your account number: ");
Acc_num = Integer.parseInt(stringAcc_num);
stringAcc_password = JOptionPane.showInputDialog("Please enter your account password: ");
Acc_password = Integer.parseInt(stringAcc_password);
if (Acc_num == acc_num && Acc_password == acc_password)
break;
}//end for
if (i > 2){
JOptionPane.showMessageDialog(null, "NO MORE TIRES!", "ALERT!", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}//end if
}//end while
if (Acc_num == acc_num && Acc_password == acc_password){
//to read the data from port-account.txt file
Scanner readFile = new Scanner (new FileReader("port-account.txt"));
//to create a txt file name "transaction-list.txt"
//PrintWriter outFile = new PrintWriter("transaction-list.txt");
int current_balance = readFile.nextInt();
do{
//to pop out a message box
String stringOption = "1. Transfer an account" + "\n2. List recent transactions" + "\n3. Display account details and current balance" + "\n4. Quit" + "\nPlease enter a number base on the following options above.";
//to convert String into Int and stores it into "option"
int option = Integer.parseInt(JOptionPane.showInputDialog(null, stringOption, "Menu options", JOptionPane.INFORMATION_MESSAGE));
switch(option){
case 1:
String Case1 = JOptionPane.showInputDialog("Please enter an amount to transfer: ");
int amount_transfered = Integer.parseInt(Case1);
int newCurrent_balance = current_balance - amount_transfered; //data from port-account.txt - user input amount
JOptionPane.showMessageDialog(null, timestamp + "\nAmount transfered: $"+amount_transfered + "\nCurrent Balance: $"+newCurrent_balance, "Amount transfer complete!", JOptionPane.INFORMATION_MESSAGE);
//print it to transaction-list.txt file
outFile.println("Current Balance: $" + newCurrent_balance);
break;
case 2:
System.out.print("testing123! testing123!");
break;
case 3:
JOptionPane.showMessageDialog(null, "\nAccount Name: "+acc_name + "\nAccount Number: "+acc_num + "\nCurrent Balance: $"+current_balance, "Account Details", JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "The number you have input is invalid. Please try again.", "ERROR", JOptionPane.INFORMATION_MESSAGE);
}//end switch
}/*end do*/ while (!quit); //repeat do loop while "!quit"(not quit).
outFile.close();
readFile.close();
}//end if
}
}
【问题讨论】:
-
这是一个相当长的代码。你到底是在哪里打印的?你在刷新输出流吗?
-
我似乎无法将要打印的内容打印到“transaction-list.txt”文件中。而且我不知道为什么,它应该有效。
-
代码中的哪一行应该打印到文件中?
-
而不是 system.exit 在选项 4 中退出循环。我想这可以完成这项工作。
-
outFile.println("你好!");和 outFile.println("Current Balance: $" + newCurrent_balance).....它不会在 transaction-list.txt 文件中打印任何内容
标签: java printwriter