Recently, a friend of mine asked me to program something for him that would make managing his small business easier. Basically the user is supposed to select the name of a client, and the code retrieves information about the client like the amount this client owes, or the next time my friend is supposed to meet with this client. Right now, my program is doing a fine job reading from the file called AccountData.txt by using this small bit of code...
myScanner = new Scanner(System.in);
diskScanner = new Scanner(AccountData);
System.out.println("Type an existing account name to get started or type 'new' to create another");
name = myScanner.next();
do {
reading = diskScanner.next();
} while(!reading.equals(name));
weekday = diskScanner.next();
day = diskScanner.nextInt();
month = diskScanner.nextInt();
Amount = diskScanner.nextDouble();
TotalAmount = diskScanner.nextDouble();
System.out.print("Next Job is ");
System.out.print(weekday);
System.out.print(" ");
System.out.print(month);
System.out.print("/");
System.out.println(day);
System.out.print("The total amount payed by this client is ");
System.out.println(TotalAmount);
System.out.print("The total amount owed is ");
System.out.println(Amount);
AddDates.AddingDates();
...but I'm having problems writing to the existing code.
public class AddDates {
private static Scanner myScanner;
static String reply;
public static void AddingDates() throws IOException {
myScanner = new Scanner(System.in);
System.out.println("Add Dates?");
reply = myScanner.next();
AccountReader.adder = new PrintWriter(new FileWriter(AccountReader.AccountData, true));
if (reply .equals("Y")) {
System.out.println("Day, Date, Month, Cost of Lawn");
AccountReader.weekday = myScanner.next();
AccountReader.day = myScanner.next();
AccountReader.month = myScanner.next();
AccountReader.Amount = myScanner.next();
AccountReader.adder.print(" ");
AccountReader.adder.print(AccountReader.weekday);
AccountReader.adder.print(" ");
AccountReader.adder.print(AccountReader.day);
AccountReader.adder.print(" ");
AccountReader.adder.print(AccountReader.month);
AccountReader.adder.print(" ");
AccountReader.adder.print(AccountReader.Amount);
AccountReader.adder.print(" ");
AccountReader.adder.print(AccountReader.TotalAmount);
AccountReader.adder.flush();
} else {
System.out.println("OK");
}
}
}
because reading from the file is location sensitive, this bit of code that writes to the file doesn't work. That is mainly because it adds the information to the end of the file. (I should also add that the AccountData.txt file looks something like this)
Masterson Saturday 22 9 50 300
Johnson Sunday 17 8 20 140 Sunday 24 8 20 140
//Etc.
So my main question is... is there anyway to write to a file and have information be written to a specific place in the file (like next to other strings) or is there another way I should write my code to make it work for my purposes?