-1

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
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mikeman44
  • 9
  • 2
  • 1
    You can't use `public static` modifier in a method (main). Remove the `public static` modifier. or make `scan` a class variable. – c0der Mar 10 '22 at 04:44
  • Also `rent * intRate = tax;` should be `tax = rent * intRate;`. The same applies to `total` and `payment`. Use `br.readLine()` instead of `br.readline()`. – c0der Mar 10 '22 at 04:50

1 Answers1

0

br.readLine() could return null the first time, so "rent" could be uninitialized. You need to handle that and the case where there is no input.

total needs to be initialized to zero.

The rest are checked exceptions when opening and writing to file that you need to handle with try/catch.

Also you are assuming nextToken() does not throw (unchecked) exception NoSuchElementException which you should not do.

Even though you "know" the input file is there and in the form you expect, your code should not (and in some cases can not) assume that. For a simple program like this when anything goes wrong you should probably print an error message to stderr and return from main or (better) exit with a non-zero value. To make this code bulletproof, it will probably end up three times the size it is now, but that is how it goes.

Or, if you just want it to "work", you can fix the uninitialized variables and declare the checked exceptions to be thrown by "main", but that is bad practice and could be the start of a bad habit.

lbarowski
  • 411
  • 3
  • 2