I am just wondering why I am getting this error and what I need to do to fix it.
ID: 3568881 • Letter: I
Question
I am just wondering why I am getting this error and what I need to do to fix it.
Program:
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
public class MileageReimbursement_3_09 {
static Toolkit tools = new Toolkit();
public static void main (String[]args)throws IOException {
Scanner input = new Scanner(System.in);
// File Names
final String OUTPUT = "MileageReimbursement_3_09_Output.txt";
final String INPUT = "MileageRemibursement_3_09_Input.txt";
// Declare Variables
final int n = 11;
double totalreimb = 0;
int ctr = 0;
double reimbursement = 0; // Reimbursement amount
double mileage = 0; // Calculated mileage
int controlVar = 0; // loop control for input file
String table; // Creates table
String line = " ";
String filename =" ";
StringTokenizer st;
BufferedReader br = new BufferedReader(new InputStreamReader
(new FileInputStream(filename)));
int[] mileagevalue = new int[n];
// Summary Statement
SummaryStatement();
// Output file being created
PrintWriter output = new PrintWriter(OUTPUT);
// access of input file
File file = new File(INPUT);
Scanner inputRead = new Scanner(file);
// output table heading
table = TableHeading();
System.out.println(table);
output.println(table);
// using first line of input for loop control
controlVar = inputRead.nextInt();
// running through contents of input file
for (int i = 0; i < n; i++)
{
line =br.readLine();
st = new StringTokenizer (line);
mileage = Double.parseDouble(st.nextToken());
// get reimbursement from calculation of mileage
if (mileage <=0)
{
System.out.println("*****");
}
else if (mileage < 400)
{
reimbursement = mileage*.18;
}
else if (mileage < 900)
{
reimbursement = 65.00 + (.15 * (mileage-400));
}
else if (mileage < 1300)
{
reimbursement = 115.00 + (.12 * (mileage-900));
}
else if (mileage < 1900)
{
reimbursement = 140.00 + (.10 * (mileage-1300));
}
else if (mileage < 2600)
{
reimbursement = 165.00 + (.08 * (mileage-1900));
}
else if (mileage >= 2600)
{
reimbursement = 195.00 + (.06 * (mileage-2600));
}
// output to output folder
output.println (leftpad(mileage,20)
+ leftpad2(reimbursement,20));
output.println (leftpad(mileage,20)
+ "*******");
{
if (mileage > 0)
totalreimb = reimbursement;
if (mileage >= 0)
ctr = ++i;}
System.out.println(mileage + " " + reimbursement);
}// end for loop
// closing input and output files
input.close();
output.close();
}// end main
//********************************************************************************************
public static void SummaryStatement()
{
System.out.println("This program reads a list of values representing number of miles"
+ " driven by individuals. " + "It will output the dollar amount their reimbursement.");
}// end SummaryStatement
//*********************************************************************************************
public static String TableHeading()
{
String holder =
("Number of miles traveled on the left as well as amount reimbursed on the right "
+ "----------------------------------------------- "
+"Miles Driven" + " " + "Amount reimbursed");
return holder;
}// end TableHeading
//*********************************************************************************************
public static String leftpad(double mileage,int w)
{
DecimalFormat formatter = new DecimalFormat ("0.0");
String s = formatter.format (mileage);
int m = s.length();
for (int i = 0; i < w; i++);
s = " " + s;
return s;}
//**********************************************************************************************
public static String leftpad2(double reimbursement,int w)
{
DecimalFormat formatter = new DecimalFormat ("0.00");
String s = formatter.format (reimbursement);
int m = s.length();
for (int i = 0; i < w; i++)
s = " " + s;
return s;
}
}// end class
It will compile, but It wont run. Here is what it says:
Exception in thread "main" java.io.FileNotFoundException: (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at MileageReimbursement_3_09.main(MileageReimbursement_3_09.java:38)
Here is the input file
10
250.6
99.4
-2.78
999.4
799.4
1899.8
0
1300.2
1101.7
3333.3
Explanation / Answer
You need to change the "fileName" in the BufferedReader object.
fileName is null and your are passing it to BufferedReader, hence the error.
Replace the BufferedReader line with the following.@line 38
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(INPUT)));
Heres the output:
This program reads a list of values representing number of miles driven by individuals.
It will output the dollar amount their reimbursement.
Number of miles traveled on the left as well as amount reimbursed on the right
-----------------------------------------------
Miles Driven Amount reimbursed
10.0 1.7999999999999998
250.6 45.108
99.4 17.892
*****
-2.78 17.892
999.4 126.928
799.4 124.91
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.