/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code1may18v1; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code1May18v1 { /** * @param args the command line arguments */ public static void main(String[] args) { final int MAXANIMALS = 10; final int MAXBREEDDATA = 5; int MAXPETS = 25; 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]; int[] animalNumbers = new int[MAXROWS]; 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]; int numOfBreeds; // value after “;” telling number of breed of this animal in the file File petsFile = new File("PetsHdr.txt"); //File petsFile = new File("PetsMix.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; animalNumbers[i] = -1; } for (int i = 0; i < MAXROWS; i++) // initialize strings for (int j = 0; j < MAXSTRCOLS; j++) { animalWords[i][j] = ""; } animalLabel = 0; 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; animalType = ""; tempLine = ""; String tempLabel = ""; while (fileValid && (validTopLabel == false) && inFile.hasNext()) { try { animalInts[index][NUMBER] = inFile.nextInt(); animalNumbers[animalLabel] = animalInts[index][NUMBER]; animalLabel++; //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("\n"); System.out.println("animalLabel is "+animalLabel); for (int i= 0; i < animalLabel; i++) { System.out.println("animalNumbers["+i+"] = "+animalNumbers[i]); } System.out.println("Searching animalNumbers for 7 "); System.out.println("row is "+findValueBinarySearch(animalNumbers, 7) ); } 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(""); } public static int findValueBinarySearch(int[] values, int searchValue) { int hi = values.length -1; int lo = 0; int mid = -1; int index = -1; boolean notDone = true; System.out.println("hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); while (notDone) { mid = (hi + lo)/2; System.out.println("hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); if (hi >= lo) { if (searchValue == values[mid]) { index = mid; notDone = false; System.out.println("hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); } else if (searchValue < values[mid]) { hi = mid - 1; System.out.println("hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); } else //if (searchValue > values[mid]) { lo = mid+1; System.out.println("hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); } } else { notDone = false; System.out.println("hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); } } return index; } }