/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package test2animals; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Test2Animals { /** * @param args the command line arguments */ public static void main(String[] args) { int animalLabel1, animalLabel2, animalLabel3; // numbers at beginnings of lines listing animal types String animalType1, animalType2, animalType3; // names of animal types that ends at “;”, ex. “Cats” int numOfBreeds; // value after “;” telling number of breed of this animal in the file String breed1, breed2, breed3; // names of the breeds for this animal in the file int breedCnt1, breedCnt2, breedCnt3; // number of animals of each breed in the file String breedLabelA = "", breedLabelB = "", breedLabelC = ""; // number-letter labels on lines of each animal breed int breedNumA = 0, breedNumB = 0, breedNumC = 0 ; // number part of a breedLabel String breedLetA = "", breedLetB = "", breedLetC = ""; // letter part of a breedLabel String breedTypeA = "", breedTypeB = "", breedTypeC = ""; // the specific breed name that ends at “;” String animalName1, animalName2, animalName3, animalName4; int animalYOB1, animalYOB2, animalYOB3, animalYOB4; // Year Of Birth = YOB 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; try { inFile = new Scanner(petsFile); } catch (FileNotFoundException fnfe) { inFile = new Scanner(System.in); fileValid = false; } animalLabel1 = 0; animalType1 = ""; boolean validTopLabel = false; int animalLabel = 0; String animalType = ""; String 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; animalLabel1 = animalLabel; animalType1 = animalType; animalLabel2 = 0; animalType2 = ""; 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; animalLabel2 = animalLabel; animalType2 = animalType; animalLabel3 = 0; animalType3 = ""; 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; animalLabel3 = animalLabel; animalType3 = animalType; System.out.println("animalLabel1 "+animalLabel1+" animalType1 "+animalType1); System.out.println("animalLabel2 "+animalLabel2+" animalType2 "+animalType2); System.out.println("animalLabel3 "+animalLabel3+" animalType3 "+animalType3); 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 == 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(); } } System.out.println("animalLabel = "+animalLabel+ " chosenAnimalType = "+chosenAnimalType+"\n"); if ((validTopLabel) && (animalLabel == chosenAnimalType)) { //System.out.println("DEBUG: In (animalLabel == chosenAnimalType)"); animalLabel = 0; breed1 = ""; breedCnt1 = 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); /* inFile.useDelimiter("[0-9]"); breed1 = inFile.next(); inFile.reset(); */ breed1 = readWith09Delim(inFile); breed1 = breed1.trim(); //System.out.println("DEBUG: breed1 is *"+breed1+"*"); breedCnt1 = inFile.nextInt(); //System.out.println("DEBUG: breedCnt1 is "+breedCnt1); String breed = ""; String nextLabel = ""; breed3 = breed2 = ""; breedCnt3 = breedCnt2 = 0; int tempNum = 0; if (inFile.hasNext() ) // second breed or next line { /* inFile.useDelimiter("[0-9]"); breed = inFile.next(); //System.out.println("DEBUG: breed is *"+breed+"*"); inFile.reset(); */ breed = readWith09Delim(inFile); //System.out.println("DEBUG: breed.length(); is *"+breed.length()+"*"); 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) { breed2 = breed; //System.out.println("DEBUG: breed2 ime is *"+breed2+"*"); breedCnt2 = inFile.nextInt(); } catch (NumberFormatException nfe) { breed2 = breed; //System.out.println("DEBUG: breed2 nfe is *"+breed2+"*"); breedCnt2 = inFile.nextInt(); } if (inFile.hasNext() ) // third breed or next line { /* inFile.useDelimiter("[0-9]"); breed = inFile.next(); inFile.reset(); */ breed = readWith09Delim(inFile); t = breed.charAt(0); tempLine = String.valueOf( t ); try { tempNum = Integer.parseInt(tempLine);// # starts new line nextLabel = breed; } catch (InputMismatchException ime) { breed3 = breed; if (inFile.hasNextInt()) breedCnt3 = inFile.nextInt(); } catch (NumberFormatException nfe) { breed3 = breed; if (inFile.hasNextInt()) breedCnt3 = inFile.nextInt(); } } } } if (nextLabel.equalsIgnoreCase("")) { nextLabel = inFile.next(); } System.out.println("\nbreed1 "+breed1+" breedCnt1 "+breedCnt1); if (breedCnt2 > 0) System.out.println("breed2 "+breed2+" breedCnt2 "+breedCnt2); if (breedCnt3 > 0) System.out.println("breed3 "+breed3+" breedCnt3 "+breedCnt3); // 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) { breedLabelA = nextLabel; breedNumA = Integer.parseInt(nextLabel.substring(0, nextLabel.length() - 2+1)); breedLetA = nextLabel.substring(nextLabel.length() - 1); } catch(NumberFormatException nfe) { breedLabelA = nextLabel; //System.out.println("DEBUG: nextLabel is "+nextLabel); breedNumA = Integer.parseInt(nextLabel.substring(0, (nextLabel.length() - 2+1))); breedLetA = nextLabel.substring(nextLabel.length() - 1); } System.out.println("\nbreedLabel1 = "+breedLabelA); //System.out.println("breedNumA = "+breedNumA+" breedLetA "+breedLetA); inFile.useDelimiter("; "); // name ends with : breedTypeA = inFile.next(); // name inFile.reset(); breedTypeA = breedTypeA.trim(); if (breed1.equalsIgnoreCase(breedTypeA)) { inFile.next(); // remove ";" /* inFile.useDelimiter("[0-9]"); animalName1 = inFile.next(); inFile.reset(); */ animalName1 = readWith09Delim(inFile); animalName1 = animalName1.trim(); animalYOB1 = inFile.nextInt(); if (breedCnt1 >= 2) { /* inFile.useDelimiter("[0-9]"); animalName2 = inFile.next(); inFile.reset(); */ animalName2 = readWith09Delim(inFile); animalName2 = animalName2.trim(); animalYOB2 = inFile.nextInt(); if (breedCnt1 >= 3) { /* inFile.useDelimiter("[0-9]"); animalName3 = inFile.next(); inFile.reset(); */ animalName3 = readWith09Delim(inFile); animalName3 = animalName3.trim(); animalYOB3 = inFile.nextInt(); if (breedCnt1 >= 4) { /* inFile.useDelimiter("[0-9]"); animalName3 = inFile.next(); inFile.reset(); */ animalName4 = readWith09Delim(inFile); animalName4 = animalName4.trim(); animalYOB4 = inFile.nextInt(); } } } } System.out.println("\nanimalName1 "+animalName1+ " animalYOB1 "+ animalYOB1); if (animalYOB2 > 0) System.out.println("animalName2 "+animalName2+ " animalYOB2 "+ animalYOB2); if (animalYOB3 > 0) System.out.println("animalName3 "+animalName3+ " animalYOB3 "+ animalYOB3); if (animalYOB4 > 0) System.out.println("animalName4 "+animalName4+ " animalYOB4 "+ animalYOB4); /* 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"); System.out.printf("%3s%20s%10d\n", 1,animalName1,animalYOB1); if (animalYOB2 != 0) System.out.printf("%3s%20s%10d\n", 2,animalName2,animalYOB2); if (animalYOB3 != 0) System.out.printf("%3s%20s%10d\n", 3,animalName3,animalYOB3); if (animalYOB4 != 0) System.out.printf("%3s%20s%10d\n", 4,animalName4,animalYOB4); } } public static String readWith09Delim(Scanner inFile) { //String value = ""; inFile.useDelimiter("[0-9]"); String value = inFile.next(); inFile.reset(); return value; } }