java - My PrintWriter is overwriting each line I print. How do I get it to log everything in my program? -
this question has answer here:
- how append text existing file in java 27 answers
when try log failed attempts printwriter overwrites each line in log instead of making multiple lines in log each failed attempt. how remedy this? what's going on it?
package hw1;
import java.io.file; import java.io.filenotfoundexception; import java.io.printwriter; import java.util.scanner; public class banking { public static void main(string[] args) throws filenotfoundexception { file fi1 = new file("clientinfo.txt"); scanner sc1 = new scanner(fi1); scanner sc2 = new scanner(system.in); scanner sc3 = new scanner(system.in); scanner loginscan = new scanner(system.in); clientrecords[] clientrecordarray = new clientrecords[100]; printwriter logtransaction1 = new printwriter("banklogs.txt"); string firstname; string middlename; string lastname; string userid = ""; string useridtyped; int pinnumber; double balance; string choice1; int choice2; int logsize = 0; int faileduserid = 0; while(sc1.hasnext()) { firstname = sc1.next(); middlename = sc1.next(); lastname = sc1.next(); userid = sc1.next(); pinnumber = sc1.nextint(); balance = sc1.nextdouble(); clientrecords client1 = new clientrecords(firstname, middlename, lastname, userid, pinnumber, balance); clientrecordarray[logsize] = client1; logsize++; } system.out.println("please enter user id:"); useridtyped = loginscan.nextline(); while(!clientrecordarray[0].getuserid().equals(useridtyped)) { useridtyped = null; system.out.println("wrong user id please try again:"); faileduserid++; useridtyped = loginscan.nextline(); } logtransaction1.println("failed enter user id " + faileduserid + " times."); logtransaction1.close(); menu1(sc2); menu2(sc3); } public static void menu1(scanner sc2) { clientrecords client1 = new clientrecords("n", "xx", "uygg", "pinkfloyd111", 0007, 51234.99); system.out.println("please enter corresponding pin number:"); int userpin = sc2.nextint(); while(userpin != client1.getuserpin()) { system.out.println("sorry wrong pin please try again."); userpin = sc2.nextint(); } } public static void menu2(scanner sc3) throws filenotfoundexception { printwriter logtransaction = new printwriter("banklogs.txt"); clientrecords client1 = new clientrecords("hjhh", "hjh", "hh", "pinkfloyd111", 0007, 51234.99); int choice = 0; double depositamount = 0; double withdrawamount = 0; double balance = client1.getuserbalance(); system.out.println("welcome bank!"); system.out.println("press (1) deposit account. "); system.out.println("press (2) withdraw account. "); system.out.println("press (3) check balance of account. "); system.out.println("press (4) exit system."); system.out.println("enter number choice here: "); choice = sc3.nextint(); switch (choice) { case 1: system.out.println("please enter how you'd desposit here:"); depositamount = sc3.nextdouble(); balance += depositamount; system.out.println("you have deposited " + "$" + depositamount); logtransaction.println("deposit of: " + "$" + depositamount); logtransaction.close(); break; case 2: system.out.println("please enter how you'd withdraw here:"); withdrawamount = sc3.nextdouble(); balance -= withdrawamount; system.out.println("you have withdrawn " + "$" + withdrawamount); logtransaction.println("withdrawal of: " + "$" + withdrawamount); logtransaction.close(); break; case 3: system.out.println("the balance of account is: " + "$" + balance); break; case 4: system.out.println("thank , have great day!"); } } }
every time call
printwriter logtransaction = new printwriter("banklogs.txt");
you truncating file , starting again. append need call filewriter explicitly.
printwriter logtransaction = new printwriter(new filewriter("banklogs.txt", true), true);
this enables appending existing file. second true
enables auto-flush less lose data if program crashes.
Comments
Post a Comment