/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package spr17apr25; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author jcmtiernan */ public class Spr17Apr25 { /** * @param args the command line arguments */ public static void main(String[] args) { // Showing file reading methods with delimiters and binary numbers final int LINEMAX = 25; int base = 10; int exponent = 0; File file = new File("bases.txt"); boolean fileFound = false; PrintWriter pw; try { pw = new PrintWriter(file); fileFound = true; } catch (FileNotFoundException ex) { pw = new PrintWriter(System.out); } if (fileFound) { pw.printf("%12s\t","Exp"); for(int b = 1; b < 11; b++) { pw.printf("%12s\t","Base"); } pw.println(); pw.printf("\t"); for (int i = 1; i < 11; i++) { pw.printf("%12d\t",i); } pw.println(); pw.println(); for(int e = 0; e < LINEMAX; e++) { pw.print(e+"\t"); for(int b = 1; b < 11; b++) { base = 1; for (int i = 0; i < e; i++) base = base * b; pw.printf("%12d\t",base); } //pw.print(((rand.nextInt(1000) % 2) == 0) ?'Y':'N'); pw.println(); } } pw.close();// be sure to close BufferedWriter double groceryBill = 0; // a running sum of price times the quantity int itemCount; // how many different prices are entered double highestPrice = -99; // highest individual price double largestQuantity = -99; // largest single quantity //String unit = ""; //String name = ""; String largestQuantityUnit = ""; String largestQuantityName = ""; String highestPriceName = ""; int ROWMAX = 30; int COLMAX = 12; int PRICE = 0; // column 0 in grocNumbers will hold the price int QUANT = 1; // column 1 in grocNumbers will hold the quantity int M = 2; int T = 3; int W = 4; int R = 5; int F = 6; int S = 7; int N = 8; int ITEMCOST = 9; int WEEKTOT = 10; int UNIT = 0; // column 0 in grocWords will hold the unit int NAME = 1; // column 1 in grocWords will hold the name int MANU = 2; // col 2 holds manufacturer name if one double[][] grocNumbers = new double[ROWMAX][COLMAX]; String[][] grocWords = new String[ROWMAX][COLMAX]; File inFile = new File("groceries.txt"); Scanner input = new Scanner(System.in); // connect to file PrintWriter screen = new PrintWriter(System.out); Scanner inData = new Scanner(System.in); // for user input from kybd boolean inFileFound = false; try { input = new Scanner(inFile); inFileFound = true; } catch (FileNotFoundException fnf) { input = new Scanner(System.in); } itemCount = 0; while (input.hasNextDouble()) { grocNumbers[itemCount][PRICE] = input.nextDouble(); // Verify the price if (grocNumbers[itemCount][PRICE] > 0) // ****Check for -1 if no file exists { // ****The program should ask the user to enter a quantity // if no file exists //if (!inFileFound) //{ System.out.println("Please enter a quantity as a whole number: "); } grocNumbers[itemCount][QUANT] = input.nextInt(); // Verify the quantity if (grocNumbers[itemCount][QUANT] > 0) { //****read string data from input file if there is a file //if (inFileFound) //{ grocWords[itemCount][UNIT] = input.next(); grocNumbers[itemCount][M] = input.nextInt(); grocNumbers[itemCount][WEEKTOT] = grocNumbers[itemCount][M]; input.useDelimiter(","); //grocNumbers[itemCount][T] = input.nextInt(); String groc = input.next(); //System.out.println("groc = "+groc); try { groc = removeBlanks(groc); grocNumbers[itemCount][T] = Integer.parseInt(groc); } catch (InputMismatchException ime) { grocNumbers[itemCount][T] = 0; } grocNumbers[itemCount][WEEKTOT] += grocNumbers[itemCount][T]; input.reset(); input.next(); // remove comma for (int day = W; day <= N; day++) { groc = removeBlanks(groc); grocNumbers[itemCount][day] = input.nextInt(); grocNumbers[itemCount][WEEKTOT] += grocNumbers[itemCount][day]; //System.out.println("grocNumbers[itemCount][day] = "+grocNumbers[itemCount][day]); } String punct = ""; String endOLine = input.nextLine(); Scanner nameScanner = new Scanner(endOLine); nameScanner.useDelimiter(";\\s*"); grocWords[itemCount][NAME] = nameScanner.next(); grocWords[itemCount][NAME] = removeBlanks(grocWords[itemCount][NAME]); nameScanner.reset(); if (nameScanner.hasNext()) { punct = nameScanner.next(); if (punct.charAt(0) == ';') { grocWords[itemCount][MANU] = nameScanner.nextLine(); grocWords[itemCount][MANU] = removeBlanks(grocWords[itemCount][MANU]); } else { grocWords[itemCount][MANU] = ""; } } else { grocWords[itemCount][MANU] = ""; } } // else for QUANT <= 0 else { itemCount--; input.nextLine(); } } // else for PRICE <= 0 else { itemCount--; input.nextLine(); } itemCount++; // } for (int z = 0; z < itemCount; z++) { outputInfo(grocNumbers, grocWords, z); } } public static String removeBlanks(String inString) { while (inString.charAt(0) == ' ') { //System.out.println("Removing blank in "+inString); inString = inString.substring(1); //System.out.println("Removed blank to "+inString); } return inString; } public static void outputInfo(double[][] grocNumbers, String[][] grocWords, int z) { int PRICE = 0; // column 0 in grocNumbers will hold the price int QUANT = 1; // column 1 in grocNumbers will hold the quantity int M = 2; int T = 3; int W = 4; int R = 5; int F = 6; int S = 7; int N = 8; int ITEMCOST = 9; int WEEKTOT = 10; int UNIT = 0; // column 0 in grocWords will hold the unit int NAME = 1; // column 1 in grocWords will hold the name int MANU = 2; // col 2 holds manufacturer name if one System.out.println(""); System.out.print(grocNumbers[z][PRICE]+" per unit for " +grocNumbers[z][QUANT]); System.out.print(" "+grocWords[z][UNIT]+" was " + grocNumbers[z][ITEMCOST] +" for "+grocWords[z][MANU]+" "+grocWords[z][NAME]); System.out.printf("\n# bought on Monday was "+grocNumbers[z][M]+","); System.out.printf(" on Tues. was "+grocNumbers[z][T]+","); System.out.printf(" on Wed. "+grocNumbers[z][W]+","); System.out.printf(" Thurs. "+grocNumbers[z][R]+","); System.out.printf(" Fri. "+grocNumbers[z][F]+","); System.out.printf(" Sat. "+grocNumbers[z][S]+","); System.out.printf(" and Sun. "+grocNumbers[z][N]); System.out.println("\nfor a total of "+grocNumbers[z][WEEKTOT]+" purchased this week."); } }