I have a text file that contains the following:
12345678,1234,100,DefaultUser
The third value "100" is a balance of money, what i need help doing is creating a withdraw method that changes that value according to the users input.
Can someone show an example of this so i can see how it could be done? This is what i have and i'm probably way off.
//Withdraw
public static void withdraw(){
//Create a scanner object
Scanner sc = new Scanner(System.in);
// Get user input
System.out.println("How much money would you like to withdraw?\n");
System.out.print("Enter amount:\t");
float amount = sc.nextInt();
try {
BufferedReader br = new BufferedReader(new FileReader("Data/users.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("Data/users.txt"));
String[] userInfo = br.readLine().split(",");
float currBalance = Integer.parseInt(userInfo[2]);
float newBalance = currBalance - amount;
userInfo[2] = ""+newBalance;
bw.write(userInfo+"");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}