/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code17apr18; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code17Apr18 { /** * @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; 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"); Scanner inFile; // assume this will be correctly assigned the File variable in a try-catch block boolean fileValid = true; boolean validTopLabel = false; try { inFile = new Scanner(petsFile); } catch (FileNotFoundException fnfe) { inFile = new Scanner(System.in); fileValid = false; } int animalLabel = 0; String animalType = ""; String tempLine = ""; 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] = ""; } for( int index = 0; (index < MAXANIMALS) && 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 = ""; while (fileValid && (validTopLabel == false) && inFile.hasNext()) { try { animalLabel = inFile.nextInt(); validTopLabel = true; inFile.useDelimiter("; "); // name ends with : animalType = inFile.next(); // name inFile.reset(); //inFile.nextLine(); } catch (InputMismatchException ime) { validTopLabel = false; tempLine = inFile.nextLine(); } } //validTopLabel = false; animalInts[index][NUMBER] = animalLabel; animalWords[index][NAME] = animalType; // End of top level line section // where "index++" happens } // bottom of for loop for(int i = 0; (i < MAXANIMALS) ; i++) { if (animalInts[i][NUMBER] != -1) { System.out.println("animalInts["+i+"] = "+animalInts[i][NUMBER]+ " animalWords ["+i+"] = "+animalWords[i][NAME]); } } /* System.out.println("Choose the label of your preferred type: "); Scanner kybd = new Scanner(System.in); int chosenAnimalType = 0; try { chosenAnimalType = kybd.nextInt(); System.out.println("You chose "+chosenAnimalType); } catch (InputMismatchException ime) { System.out.println("Invalid choice. Default is choice 1."); chosenAnimalType = 1; } 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; } }