/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code26april18; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code26April18 { /** * @param args the command line arguments */ public static void main(String[] args) { final int MAXANIMALS = 10; final int MAXBREEDDATA = 5; int MAXPETS = 25; //int animalLabel1, animalLabel2, animalLabel3; // numbers at beginnings of lines listing animal types /* int[] animalLabels = new int[MAXANIMALS]; int[] breedCnts = new int[MAXBREEDDATA]; int[] breedNums = new int[MAXBREEDDATA]; int[] animalYOBs = new int[MAXPETS]; */ final int MAXROWS = 60; // maxanimals + (maxbreeddata * maxanimals) final int MAXINTCOLS = 27; // number + breedcount + maxpets final int NUMBER = 0; final int BREEDCOUNT = 1; int[][] animalInts = new int[MAXROWS][MAXINTCOLS]; final int MAXSTRCOLS = 27; // name + subletter + maxpets final int NAME = 0; final int LETTER = 1; final int ABBREV = 2; String [][] animalWords = new String[MAXROWS][MAXSTRCOLS]; //String animalType1, animalType2, animalType3; // names of animal types that ends at “;”, ex. “Cats” /* String[] animalTypes = new String[MAXANIMALS]; String[] breeds = new String[MAXBREEDDATA]; String[] breedLabels = new String[MAXBREEDDATA]; String[] breedLets = new String[MAXBREEDDATA]; String[] breedTypes = new String[MAXBREEDDATA]; String[] animalNames = new String[MAXPETS]; */ int numOfBreeds; // value after “;” telling number of breed of this animal in the file //File petsFile = new File("pets.txt"); File petsFile = new File("petsHmix.txt"); Scanner inFile; // assume this will be correctly assigned the File variable in a try-catch block boolean fileValid = true; boolean validTopLabel = false; //String delim = "; "; String delim = ": "; try { inFile = new Scanner(petsFile); } catch (FileNotFoundException fnfe) { inFile = new Scanner(System.in); fileValid = false; } int animalLabel = 0; String animalType = ""; String tempLine = ""; int topLevelLabels = 2; // counting the number of possible labels int subLevelLabels = 3; // counting the number of possible labels for (int i = 0; i < MAXROWS; i++) // initialize ints to -1 for (int j = 0; j < MAXINTCOLS; j++) { animalInts[i][j] = -1; } for (int i = 0; i < MAXROWS; i++) // initialize strings for (int j = 0; j < MAXSTRCOLS; j++) { animalWords[i][j] = ""; } Scanner inLine = new Scanner(System.in); for( int index = 0; (index < MAXROWS) && inFile.hasNextLine(); index++) { // section of code to read a top level line and save the // the number and name from the top level line validTopLabel = false; animalLabel = 0; animalType = ""; tempLine = ""; String tempLabel = ""; while (fileValid && (validTopLabel == false) && inFile.hasNext()) { try { animalInts[index][NUMBER] = inFile.nextInt(); System.out.println("animalInts["+index+"][NUMBER] is "+animalInts[index][NUMBER]); validTopLabel = true; inFile.useDelimiter(delim); // name ends with : animalWords[index][NAME] = inFile.next(); // name inFile.reset(); inFile.next(); // reads out the delimiter animalWords[index][ABBREV] = inFile.next().trim(); // how do you prevent reading into the next line?? inLine = new Scanner(inFile.nextLine()); for (int next=1; (next ('A'+ petCount - 1 ))) { System.out.println("Invalid choice. Default is choice A."); chosenPet = 'A'; } else { System.out.println("You chose "+chosenPet); } //int subLevelRow = (int)chosenPet - (int)'A' +topLevelRow+1; System.out.println(""); /* // ------------------- output System.out.println("\n"); makeLine('^', topLevelLabels, subLevelLabels); //String catnm = makecategories(itemWords[TopNumberIndex][ABBREV].charAt(0),categoryLtrs, categoryNames, MAXCATEGORIES); //System.out.printf("%-35s",catnm); System.out.printf("\n%4d%-30s",animalInts[topLevelRow][NUMBER], animalWords[topLevelRow][NAME]); for (int next = 1; next < topLevelLabels+1; next++) { if (!animalWords[topLevelRow][ABBREV+next].equalsIgnoreCase("")) System.out.printf("%-10s",animalWords[topLevelRow][ABBREV+next]); } System.out.println(""); makeLine('-', topLevelLabels, subLevelLabels); //System.out.printf("%63s\n","----------------------------------------------------------------"); //animalInts[index][NUMBER+next] System.out.printf("\n%38s",""); for (int next = 1; next < topLevelLabels+1; next++) { if (!animalWords[topLevelRow][ABBREV+next].equalsIgnoreCase("")) System.out.printf("%-10s",animalInts[topLevelRow][NUMBER+next]); } System.out.println(""); //.......... System.out.printf("\n%4s%-30s",animalWords[subLevelRow][LETTER], animalWords[subLevelRow][NAME]); for (int next = 1; next < subLevelLabels+1; next++) { if (!animalWords[subLevelRow][ABBREV+next].equalsIgnoreCase("")) System.out.printf("%-10s",animalWords[subLevelRow][ABBREV+next]); } System.out.println(""); makeLine('.', topLevelLabels, subLevelLabels); //System.out.printf("%63s\n","----------------------------------------------------------------"); //animalInts[index][NUMBER+next] System.out.printf("\n%38s",""); for (int next = 1; next < subLevelLabels+1; next++) { if (!animalWords[subLevelRow][ABBREV+next].equalsIgnoreCase("")) System.out.printf("%-10s",animalInts[subLevelRow][NUMBER+next]); } System.out.println(""); */ /* System.out.printf("%-35s",TopName); for (int l = 0; (STARTPAIRS+l) < MAXLABELS; l++) { //System.out.println("In data print, l is "+l+" and (STARTPAIRS+l) is "+(STARTPAIRS+l)); if (itemInts[TopNumberIndex][STARTPAIRS+l] != -1) { //System.out.println("in if, In data print"); System.out.printf("%-10s",itemInts[TopNumberIndex][STARTPAIRS+l]); } else { System.out.printf("%-10s",""); } } System.out.println(""); if (!SubName.equalsIgnoreCase("")) { System.out.printf("\n%5s%-30s","",SubName); for (int l = 0; (STARTPAIRS+l) < MAXLABELS; l++) { System.out.printf("%-10s", ( (itemInts[subNumberIndex][STARTPAIRS+l] != -1? itemWords[subNumberIndex][STARTPAIRS+l]: "") )); } System.out.println(""); makeLine('.', topLevelLabels, subLevelLabels); //System.out.printf("%63s\n","................................................................\n"); System.out.printf("%35s",""); for (int l = 0; (STARTPAIRS+l) < MAXLABELS; l++) { if (itemInts[subNumberIndex][STARTPAIRS+l] != -1) System.out.printf("%-10s",itemInts[subNumberIndex][STARTPAIRS+l]); else System.out.printf("%-10s",""); } System.out.println(""); */ /* fileValid = true; //inFile.close(); try { inFile = new Scanner(petsFile); } catch (FileNotFoundException fnfe) { inFile = new Scanner(System.in); fileValid = false; } validTopLabel = false; // need to go back to top of file and read to the desired animalType animalLabel = 0; while (fileValid && ( !validTopLabel ) //(validTopLabel == false) && inFile.hasNext() && (animalLabel != chosenAnimalType) ) { try { animalLabel = inFile.nextInt(); if (animalLabel == chosenAnimalType) validTopLabel = true; inFile.useDelimiter("; "); // name ends with : animalType = inFile.next(); // name inFile.reset(); //inFile.nextLine(); } catch (InputMismatchException ime) { validTopLabel = false; tempLine = inFile.nextLine(); } } // we have moved to the line of the user's choice System.out.println("animalLabel = "+animalLabel+ " chosenAnimalType = "+chosenAnimalType+"\n"); if ((validTopLabel) && (animalLabel == chosenAnimalType)) { //System.out.println("DEBUG: In (animalLabel == chosenAnimalType)"); animalLabel = 0; breeds[0] = ""; // breed1 = ""; breedCnts[0] = 0; // breedCnt1 = 0; breedTypes[0] = ""; // breedTypeA = ""; //animalName1 = animalName2 = animalName3 = animalName4 = ""; //animalYOB1 = animalYOB2 = animalYOB3 = animalYOB4 = 0; // then after reading past animalLabel, Type and ';', then read breeds // read number (label) and animal type // now reading number of breeds, then breed names and number of animals tempLine = inFile.next(); // reads ';' //System.out.println("DEBUG: templine should have semicolon *"+tempLine+"*"); numOfBreeds = inFile.nextInt(); System.out.println("numOfBreeds "+numOfBreeds); //breeds[0] = readWith09Delim(inFile); //System.out.println("DEBUG: breed1 is *"+breed1+"*"); //breedCnts[0] = inFile.nextInt(); //System.out.println("DEBUG: breedCnt1 is "+breedCnt1); String breed = ""; String nextLabel = ""; //breed3 = breed2 = ""; //breedCnt3 = breedCnt2 = 0; Scanner animalBreedsLine = new Scanner(inFile.nextLine()); for( int i = 0; i < MAXBREEDDATA ; i++) { breeds[i] = ""; breedCnts[i] = 0; int tempNum = 0; if (animalBreedsLine.hasNext() ) // second breed or next line { breed = readWith09Delim(animalBreedsLine); breed = breed.trim(); //System.out.println("DEBUG: breed.length(); is *"+breed.length()+"*"); if (breed.length() > 0) { char t = breed.charAt(0); if (t != '\n') { tempLine = String.valueOf( t ); //System.out.println("DEBUG: tempLine is *"+tempLine+"*"); try { tempNum = Integer.parseInt(tempLine);// # starts new line nextLabel = breed; } catch (InputMismatchException ime) { breeds[i] = breed; //System.out.println("DEBUG: breed2 ime is *"+breed2+"*"); breedCnts[i] = animalBreedsLine.nextInt(); } catch (NumberFormatException nfe) { breeds[i] = breed; //System.out.println("DEBUG: breed2 nfe is *"+breed2+"*"); breedCnts[i] = animalBreedsLine.nextInt(); } } } } } if (nextLabel.equalsIgnoreCase("")) { nextLabel = inFile.next(); } for (int index = 0; index < MAXBREEDDATA; index++ ) { if (breedCnts[index] > 0) System.out.println("breeds["+index+"] \t"+breeds[index]+ "\t breedCnts["+index+"] \t"+breedCnts[index]); } //breeds[i] = breed; //breedCnts[i] = inFile.nextInt(); // now nextLabel has the beginning of new line of data int labelValue = 0; try { labelValue = Integer.parseInt(nextLabel); // new animal line - no breed lines //animalLabel2 = labelValue; } catch(InputMismatchException ime) { breedLabels[0] = nextLabel; breedNums[0] = Integer.parseInt( nextLabel.substring(0, nextLabel.length() - 2+1)); breedLets[0] = nextLabel.substring(nextLabel.length() - 1); } catch(NumberFormatException nfe) { breedLabels[0] = nextLabel; //System.out.println("DEBUG: nextLabel is "+nextLabel); breedNums[0] = Integer.parseInt(nextLabel.substring(0, (nextLabel.length() - 2+1))); breedLets[0] = nextLabel.substring(nextLabel.length() - 1); } System.out.println("\nbreedLabelA = "+breedLabels[0]); System.out.println("breedNumA = "+breedNums[0]+" breedLetA "+breedLets[0]); inFile.useDelimiter("; "); // name ends with : breedTypes[0] = inFile.next(); // name inFile.reset(); breedTypes[0] = breedTypes[0].trim(); System.out.println("\nbreedTypeA = "+breedTypes[0]); inFile.next(); for (int x = 0; x < MAXPETS; x++) { if (breeds[0].equalsIgnoreCase(breedTypes[0])) { if (breedCnts[0] >= x+1) {// remove ";" //inFile.next(); animalNames[x] = readWith09Delim(inFile); animalNames[x] = animalNames[x].trim(); animalYOBs[x] = inFile.nextInt(); } } } for( int i = 0; i < MAXPETS; i++ ) { if (animalYOBs[i] > 0) System.out.println("animalName4 \t"+animalNames[i]+ "\t animalYOB4 \t"+ animalYOBs[i]); } */ /* animalName1, animalName2, animalName3, animalName4, animalYOB1, animalYOB2, animalYOB3, and animalYOB4 and that Name1 goes with YOB1, etc. Write the Java code to output these values in a table. The table should have three columns with the first column being a number (1, 2, 3, and 4), the second column being Name# and the third column being YOB#. The table should have 5 rows where the first row is titles of “”, “Name”, and “Born In”. The first column should be 3 characters wide, the second column should be 20 chars wide, and the last column should be 10 chars wide. */ /* System.out.println(""); System.out.printf("%3s %-20s%10s\n","", "Name", "Born In"); for (int i = 0; i < MAXPETS; i++ ) { if (animalYOBs[i] != 0) System.out.printf("%3s. %-20s%10d\n", i+1 ,animalNames[i],animalYOBs[i]); } */ } public static String readWith09Delim(Scanner inFile) { //String value = ""; inFile.useDelimiter("[0-9]"); String value = inFile.next(); inFile.reset(); return value; } public static void makeLine(char symbol, int choiceCount, int subchoiceCount) { // space for name and number is 35 spaces // each subtitle after is 10 spaces //System.out.println("symbol is "+symbol+" choiceCount = "+choiceCount+ // " subchoiceCount = "+ subchoiceCount); String temp; int c = 0; for (c = 0; c < 35; c++) System.out.print(symbol); c = 0; int k = 0; int max = 0; if (choiceCount < subchoiceCount) max = subchoiceCount; else max = choiceCount; while (c < max) { for (k = 0; k < 10; k++) System.out.print(symbol); c++; } System.out.println(""); } }