I am trying to store multiple user inputs as a byte based file and then take the files numbers and use them for calculation in the program I have written. I started by converting the string to a double, but the I ran into the issue of reading the values and subbing in the the calc methods. I figured out that I am suppose to use fileOutputStream but have no idea how to incorporate into the code. Any guidance would be appreciated. Please see code for reference:
import java.util.Scanner;
import java.io.*;
public class BankApp {
public static void main (String [] args) throws IOException{
double cash = 0;
int years = 0;
double pv = 0;
String line = null;
String filename = "bank.txt";
Scanner in = new Scanner(System.in);
System.out.println("How much money would you like to have?");
cash = in.nextDouble();
System.out.println("How many years is your investment?");
years = in.nextInt();
Bank1 bank1 = new Bank1(cash, years);
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
bw.write(String.valueOf(cash));
bw.close();
BufferedReader br = new BufferedReader(new FileReader(filename));
while((line = br.readLine()) != null)
{
//System.out.println(line);
}
System.out.println("The present value to invest over the years to reach goal: $"+bank1.presentValue());
System.out.println("The furture value in the account will be: $"+bank1.futureValue());
}
}
Here is the second class that does all the calculations.
public class Bank1 {
private double cash;
private double rate = .0425;
private int years;
public Bank1(double cash, int years){
this.cash = cash;
this.years = years;
}n
public double presentValue(){
double pv = cash/Math.pow((1+rate), years);
double present = Math.round(pv *100);
present = present/100;
return present;
}
public double futureValue(){
double fv = this.presentValue()*Math.pow((1+rate), years);
double future = Math.round(fv *100);
future = future/100;
return future;
}
}
Your best option is to read up on the different types of Character / Byte streams in the java documentation, I usually go there every time I read/write data to find one that suits my need.
http://docs.oracle.com/javase/7/docs/api/java/io/package-summary.html
(This is the documentation for the java.io package in your code you say import java.io.*
that gives you access to anything listed in the documentation)
When you say "I figured out that I am suppose to use fileOutputStream but have no idea how to incorporate into the code." you don't HAVE to unless this a assignment that requires it, I would use a FileWriter or ObjectOutputStream. ObjectOutputStream would be perfect for what you are trying to do.