/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code24apr18; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code24Apr18 { /** * @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("petsH.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 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 < 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(); 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?? for (int next=1; (next 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; } }