/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code2may18; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code2May18 { /** * @param args the command line arguments */ public static void main(String[] args) { final int MAXANIMALS = 15; final int MAXBREEDDATA = 5; int MAXPETS = 25; final int MAXROWS = 90; // 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"); //File petsFile = new File("SciFiFantasyYearHdr.txt"); //File petsFile = new File("SciFiFantasySearchable.txt"); File petsFile = new File("SciFiFantasySearchable2.txt"); //File petsFile = new File("PetsSearchable2.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); //System.out.println("Got file"); } catch (FileNotFoundException fnfe) { inFile = new Scanner(System.in); fileValid = false; System.out.println("No file"); } 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; animalWords[i][j] = ""; } /* for (int i = 0; i < MAXROWS; i++) // initialize strings for (int j = 0; j < MAXSTRCOLS; j++) { System.out.println("Element: "+animalInts[i][j]+" *"+animalWords[i][j]+"*"); } */ Scanner inLine = new Scanner(System.in); for( int index = 0; (index < MAXROWS) && inFile.hasNextLine(); index++) { //System.out.println("in for loop ** index is "+index); //System.out.println("animalInts["+index+"][NUMBER] is "+animalInts[index][NUMBER]); // 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; animalWords[index][NAME] = inFile.nextLine(); // name //System.out.println("animalWords["+index+"][NAME] is *"+animalWords[index][NAME]+"*"); } // End of top level line section // where "index++" happens } // bottom of for loop printNumberName(animalInts, animalWords, MAXROWS); System.out.println("\n"); System.out.println("animalLabel is "+animalLabel); int[] aniNumsOnly = new int[animalLabel]; for (int i= 0; i < animalLabel; i++) { // the array animalNumbers has blank spaces, i.e. -1 values, in it // we want only the actual values so we are making a new array // and storing the values into it aniNumsOnly[i] = animalNumbers[i]; //System.out.println("aniNumsOnly["+i+"] = "+aniNumsOnly[i]); } Scanner kybd = new Scanner(System.in); System.out.println("What integer value would you like to search for in the number array?"); int userChoice = kybd.nextInt(); System.out.println("\nSearching unsorted arrays for "+userChoice); System.out.println("Binary Search : row is "+(findValueBinarySearch(aniNumsOnly, userChoice) +1) ); System.out.println("Linear Search : row is "+(findValueLinearSearch(aniNumsOnly, userChoice) +1) ); bubbleSortNumberName(animalInts, animalWords, animalLabel); for (int i= 0; i < animalLabel; i++) { // the array animalNumbers has blank spaces, i.e. -1 values, in it // we want only the actual values so we are making a new array // and storing the values into it aniNumsOnly[i] = animalInts[i][NUMBER]; //System.out.println("aniNumsOnly["+i+"] = "+aniNumsOnly[i]); } int row = -1; printNumberNameRow(animalInts, animalWords, animalLabel); System.out.println("\nSearching sorted arrays for "+ userChoice); System.out.println("Binary 2D Search : row is "+(findValue2DBinarySearch(animalInts, userChoice, NUMBER, animalLabel) +1)); System.out.println("Linear 2D Search : row is "+((row = findValue2DLinearSearch(animalInts, userChoice, NUMBER, animalLabel)) +1) ); System.out.println("\nRe-sorting by names:"); String userChWords = animalWords[row][NAME]; System.out.println("User's choice of name is "+userChWords); bubbleSortNameNumber(animalInts, animalWords, animalLabel); printNumberNameRow(animalInts, animalWords, animalLabel); System.out.println("\nBinary String 2D Search : row is "+(findString2DBinarySearch(animalWords, userChWords, NAME, animalLabel) +1)); System.out.println("Linear String 2D Search : row is "+(findString2DLinearSearch(animalWords, userChWords, NAME, animalLabel) +1) ); } 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 findValueLinearSearch(int[] itemInts, int match) { int loc = -1; int i = 0; for (; i< itemInts.length; i++) if (itemInts[i] == match) { loc = i; } return loc; } public static int findValue2DLinearSearch(int[][] itemInts, int match, int COL, int max) { int loc = -1; int i = 0; for (; i< max; i++) if (itemInts[i][COL] == match) { loc = i; } return loc; } public static int findString2DLinearSearch(String[][] itemWords, String match, int COL, int max) { int loc = -1; int i = 0; for (; i< max; i++) { //System.out.println("array word *"+itemWords[i][COL]+"* compared to user choice #"+match+"#"); if (itemWords[i][COL].equalsIgnoreCase(match)) { loc = i; } } return loc; } 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("Start: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); while (notDone) { mid = (hi + lo)/2; //System.out.println("in while: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); if (hi >= lo) { if (searchValue == values[mid]) { index = mid; notDone = false; //System.out.println("search == val: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); } else if (searchValue < values[mid]) { hi = mid - 1; //System.out.println("search < val: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); } else //if (searchValue > values[mid]) { lo = mid+1; //System.out.println("search > val: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); } } else { notDone = false; //System.out.println("hi >= lo: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); } } return index; } public static int findValue2DBinarySearch(int[][] values, int searchValue, int COL, int max) { int hi = max -1; int lo = 0; int mid = -1; int index = -1; boolean notDone = true; //System.out.println("Start: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); while (notDone) { mid = (hi + lo)/2; //System.out.println("in while: hi = "+hi+", lo = "+lo+", mid = "+mid+", values[mid][COL] = "+values[mid][COL]); if (hi >= lo) { if (searchValue == values[mid][COL]) { index = mid; notDone = false; //System.out.println("search == val: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index+", values[mid][COL] = "+values[mid][COL]); } else if (searchValue < values[mid][COL]) { hi = mid - 1; //System.out.println("search < val: hi = "+hi+", lo = "+lo+", mid = "+mid+", values[mid][COL] = "+values[mid][COL]); } else //if (searchValue > values[mid][COL]) { lo = mid+1; //System.out.println("search > val: hi = "+hi+", lo = "+lo+", mid = "+mid+", values[mid][COL] = "+values[mid][COL]); } } else { notDone = false; //System.out.println("hi >= lo: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index+", values[mid][COL] = "+values[mid][COL]); } } return index; } public static int findString2DBinarySearch(String[][] values, String searchValue, int COL, int max) { int hi = max -1; int lo = 0; int mid = -1; int index = -1; boolean notDone = true; //System.out.println("Start: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index); while (notDone) { mid = (hi + lo)/2; //System.out.println("in while: hi = "+hi+", lo = "+lo+", mid = "+mid+", values[mid][COL] = "+values[mid][COL]); if (hi >= lo) { //if (searchValue.equalsIgnoreCase(values[mid][COL])) if (searchValue.compareTo(values[mid][COL]) == 0) { index = mid; notDone = false; //System.out.println("search == val: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index+", values[mid][COL] = "+values[mid][COL]); } else if (searchValue.compareTo(values[mid][COL]) < 0) { hi = mid - 1; //System.out.println("search < val: hi = "+hi+", lo = "+lo+", mid = "+mid+", values[mid][COL] = "+values[mid][COL]); } else //if (searchValue > values[mid][COL]) { lo = mid+1; //System.out.println("search > val: hi = "+hi+", lo = "+lo+", mid = "+mid+", values[mid][COL] = "+values[mid][COL]); } } else { notDone = false; //System.out.println("hi >= lo: hi = "+hi+", lo = "+lo+", mid = "+mid+", index = "+index+", values[mid][COL] = "+values[mid][COL]); } } return index; } public static void bubbleSortNumberName(int[][] numbers,String[][] names, int maxSize) { // number and names should stay linked // sorting list by number // bubble sort for simplicity int temp; String tempString; for (int i = 0; i < maxSize; i++) for (int j = 0; j < maxSize - 1; j++) { if ((numbers[j][0] > numbers[j+1][0]) && (numbers[j+1][0] != 0)) { // swap both arrays together temp = numbers[j+1][0]; tempString = names[j+1][0]; numbers[j+1][0] = numbers[j][0]; names[j+1][0] = names[j][0]; numbers[j][0] = temp; names[j][0] = tempString; } } } public static void bubbleSortNameNumber(int[][] numbers,String[][] names, int maxSize) { // number and names should stay linked // sorting list by number // bubble sort for simplicity int temp; String tempString; for (int i = 0; i < maxSize; i++) for (int j = 0; j < maxSize - 1; j++) { if ((names[j][0].compareTo(names[j+1][0]) > 0) && (numbers[j+1][0] != 0)) { // swap both arrays together tempString = names[j+1][0]; temp = numbers[j+1][0]; names[j+1][0] = names[j][0]; numbers[j+1][0] = numbers[j][0]; names[j][0] = tempString; numbers[j][0] = temp; } } } public static void printNumberName(int[][] numbers,String[][] names, int maxSize) { final int NUMBER = 0; final int NAME = 0; final int LETTER = 2; for(int i = 0; (i < maxSize) ; i++) { if ((numbers[i][NUMBER] != -1) && (names[i][LETTER] == "")) { System.out.printf("\n%4d%30s",numbers[i][NUMBER], names[i][NAME]); } } } public static void printNumberNameRow(int[][] numbers,String[][] names, int maxSize) { final int NUMBER = 0; final int NAME = 0; final int LETTER = 2; for(int i = 0; (i < maxSize) ; i++) { if ((numbers[i][NUMBER] != -1) && (names[i][LETTER] == "")) { System.out.printf("\n%4d. %4d%30s",(i+1),numbers[i][NUMBER], names[i][NAME]); } } } }