So I'm trying to read 11 values from a text file, each value on a separate line. The first value I use as loop control to run through calculations on the other ten and finally output both the numbers and the calculations to the console and an output file. I'm not getting a compiler error or a runtime error but my loop seems to stop after reading the first line. It also doesnt seem to be writing to my output file but does create it when I run the program. This is what my text file looks like
10
150.4
88.4
-3.14
499.4
799.4
1299.8
0
1900.2
901.7
4444.4
This is what my program looks like
import java.util.*; import java.io.*; public class assignment7scratch { Toolkit_General toolKit = new Toolkit_General(); public static void main (String[]args)throws IOException { double rentalCost = 0; // holds cost calculated from mileage double mileage = 0; // miles driven int controlVar = 0; // loop control for going through input file String inputFile = "QudratullahMommandi_3_07_Input.txt"; // Summary Statement SummaryStatement(); //Output file being created PrintWriter outPut = new PrintWriter("QudratullahMommandi_3_07_Output.txt"); // input file being accessed File file = new File(inputFile); Scanner inputRead = new Scanner(file); // outputting a table heading TableHeading(); // using the first line of input for loop control controlVar = inputRead.nextInt(); // running through the contents of the input file for (int index = 0; index < controlVar; index++); { // getting value from input mileage = inputRead.nextDouble(); // construct to get the rentalCost calculation from mileage if (mileage <=0) { System.out.println(mileage + "\t\t\t\t\t" + "*****"); } else if (mileage < 400) { rentalCost = mileage*.18; System.out.println(mileage + "\t\t\t\t\t" + rentalCost); } else if (mileage < 900) { rentalCost = 65.00 + (.15 * (mileage-400)); System.out.println(mileage + "\t\t\t\t\t" + rentalCost); } else if (mileage < 1300) { rentalCost = 115.00 + (.12 * (mileage-800)); System.out.println(mileage + "\t\t\t\t\t" + rentalCost); } else if (mileage < 1900) { rentalCost = 140.00 + (.10 * (mileage-1200)); System.out.println(mileage + "\t\t\t\t\t" + rentalCost); } else if (mileage < 2600) { rentalCost = 165.00 + (.08 * (mileage-1800)); System.out.println(mileage + "\t\t\t\t\t" + rentalCost); } else if (mileage >= 2600) { rentalCost = 195.00 + (.06 * (mileage-2500)); System.out.println(mileage + "\t\t\t\t\t" + rentalCost); } // outputting to output folder outPut.println(mileage + "\t\t\t\t\t" + rentalCost); }// end for loop // closing input and output files inputRead.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.\n" + "It will output the dollar amount their rental cars cost."); }// end SummaryStatement public static void TableHeading() { System.out.println(); System.out.println("Number of miles traveled on the left as well as amount reimbursed on the right"); System.out.println("-----------------------------------------------"); System.out.println("Miles Driven" + " " + "Amount reimbursed"); } }// end class
so I dont get an error but this is what my output looks like
----jGRASP exec: java assignment7scratch
This program reads a list of values representing number of miles driven by individuals.
It will output the dollar amount their rental cars cost.
Number of miles traveled on the left as well as amount reimbursed on the right
-----------------------------------------------
Miles Driven Amount reimbursed
150.4 27.072
----jGRASP: operation complete.
it also doesn't write anything to my output file, though it does create one.