I'm trying to read data from a file, output the data to a different file, and perform calculations with the data from the input file and output them. I'm trying to use StringTokenizer to get the numbers for the text file and I've never used that before. here is my code so far.
import java.io.*;
import java.util.*;
public class project3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader("d:/Data/Project3.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("d:/Data/Project3OutData.txt"));
String str;
double tax;
double total;
double payment;
double rent;
System.out.println("Enter the interest rate: ");
double intRate = scan.nextDouble();
System.out.println("Enter months for the loan: ");
int months = scan.nextInt();
try
{
while ((str = br.readLine ()) != null)
{
StringTokenizer st1 = new StringTokenizer(str);
String name = st1.nextToken();
rent = Double.parseDouble(st1.nextToken());
bw.write(str);
bw.write(System.lineSeparator());
}
tax = rent * intRate;
total = rent + tax;
payment = total / months;
}
catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
bw.write("Number of months of the loan: " + months + "\n");
bw.write("Your payment amount each month is: " + total);
}
}
This is the input file
Duster 425
Gomer 200
Wades 450
Stines 175
These are the errors I'm getting
---jGRASP exec: javac -g project88.java
project88.java:35: error: variable rent might not have been initialized
tax = rent * intRate;
^
project88.java:44: error: variable total might not have been initialized
bw.write("Your payment amount each month is: " + total);
^
project88.java:11: error: unreported exception FileNotFoundException;
must be caught or declared to be thrown
BufferedReader br = new BufferedReader(new
FileReader("d:/Data/Project3.txt"));
^
project88.java:12: error: unreported exception IOException; must be
caught or declared to be thrown
BufferedWriter bw = new BufferedWriter(new
FileWriter("d:/Data/Project3OutData.txt"));
^
project88.java:43: error: unreported exception IOException; must be
caught or declared to be thrown
bw.write("Number of months of the loan: " + months + "\n");
^
project88.java:44: error: unreported exception IOException; must be
caught or declared to be thrown
bw.write("Your payment amount each month is: " + total);
^
6 errors