获取用户输入并存储到文件中的数组
Take user input and store to array in a file
我正在尝试将多个用户输入存储为基于字节的文件,然后获取文件编号并在我编写的程序中使用它们进行计算。我首先将字符串转换为双精度数,但我 运行 进入了读取值和在 calc 方法中替换的问题。我发现我应该使用 fileOutputStream 但不知道如何合并到代码中。任何指导将不胜感激。请看代码参考:
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());
}
}
这是执行所有计算的第二个 class。
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;
}
}
你最好的选择是阅读 java 文档中不同类型的字符/字节流,我通常每次 read/write 数据时都会去那里找到适合我需要的.
http://docs.oracle.com/javase/7/docs/api/java/io/package-summary.html
(这是您代码中 java.io 包的文档 import java.io.*
,它使您可以访问文档中列出的任何内容)
当您说 "I figured out that I am suppose to use fileOutputStream but have no idea how to incorporate into the code." 时,除非这是一项需要它的作业,否则您不必这样做,我会使用 FileWriter 或 ObjectOutputStream。 ObjectOutputStream 将非常适合您正在尝试做的事情。
我正在尝试将多个用户输入存储为基于字节的文件,然后获取文件编号并在我编写的程序中使用它们进行计算。我首先将字符串转换为双精度数,但我 运行 进入了读取值和在 calc 方法中替换的问题。我发现我应该使用 fileOutputStream 但不知道如何合并到代码中。任何指导将不胜感激。请看代码参考:
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());
}
}
这是执行所有计算的第二个 class。
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;
}
}
你最好的选择是阅读 java 文档中不同类型的字符/字节流,我通常每次 read/write 数据时都会去那里找到适合我需要的.
http://docs.oracle.com/javase/7/docs/api/java/io/package-summary.html
(这是您代码中 java.io 包的文档 import java.io.*
,它使您可以访问文档中列出的任何内容)
当您说 "I figured out that I am suppose to use fileOutputStream but have no idea how to incorporate into the code." 时,除非这是一项需要它的作业,否则您不必这样做,我会使用 FileWriter 或 ObjectOutputStream。 ObjectOutputStream 将非常适合您正在尝试做的事情。