/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code6novf18t2; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code6NovF18T2 { /** * @param args the command line arguments */ public static void main(String[] args) { File inFile = new File("Test2ArrayData.txt"); Scanner input = new Scanner(System.in); try { input = new Scanner(inFile); } catch (FileNotFoundException fnfe) { System.out.println("File not found."); } double[] lineData = new double[4]; double[] bkValues = new double[50]; int counter = 0; String temp = ""; /* Declare a two dimensional array called allBookData that could hold all 4 of the values from every line and the final book values that are calculated for those 4 values (5 total values) for each line of the file. After the array is declared, (re)write the lines of code that would read the input values from one line as doubles and store the values into this 2D array, i.e. rewrite the {input 8 pts} section from question 1.a so that it stores into the rows in allBookData instead of into lineData. {declaration 3 pts; input 5 pts} */ double[][] allBookData = new double[50][5]; while (input.hasNext()) { allBookData[counter][0] = input.nextDouble(); allBookData[counter][1] = input.nextDouble(); allBookData[counter][2] = input.nextDouble(); allBookData[counter][3] = input.nextDouble(); /* lineData[0] = input.nextDouble(); lineData[1] = input.nextDouble(); lineData[2] = input.nextDouble(); lineData[3] = input.nextDouble(); temp = input.next(); lineData[0] = Double.parseDouble(temp); temp = input.next(); lineData[1] = Double.valueOf(temp); temp = input.next(); lineData[2] = Double.parseDouble(temp); temp = input.next(); lineData[3] = Double.valueOf(temp); */ System.out.println("DEBUG: values read are: "+allBookData[counter][0]+" "+ allBookData[counter][1]+" "+allBookData[counter][2]+" "+allBookData[counter][3]+" "); //System.out.println("DEBUG: values read are: "+lineData[0]+" "+ // lineData[1]+" "+lineData[2]+" "+lineData[3]+" "); //bkValues[counter] = (lineData[0]* lineData[1]) + // (lineData[0]* lineData[3]* lineData[2]); allBookData[counter][4] = allBookData[counter][0]* allBookData[counter][1]+ (allBookData[counter][0]*allBookData[counter][2]*allBookData[counter][3]); System.out.println("DEBUG: book value (allBookData[counter][4]) = "+allBookData[counter][4]); counter++; } // calculate total value of all books as the // sum of all values in the book values array. double totalValue = 0; for (int i = 0; i < counter; i++) { //totalValue = totalValue + bkValues[i]; //totalValue += bkValues[i]; totalValue += allBookData[i][4]; } System.out.printf("Total books value is $%.2f\n",totalValue); } }