/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package finalrevprep2s19; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author jcmtiernan */ public class FinalRevPrep2S19 { /** * @param args the command line arguments */ public static void main(String[] args) { String inputFileName = "catAges.txt"; File nameAgeFile = new File(inputFileName); Scanner inFile = new Scanner(System.in); try { inFile = new Scanner(nameAgeFile); } catch (FileNotFoundException fnfe) { System.out.println("No input data file found"); } ArrayList cats = new ArrayList<>(); cats = catlove(5, inFile); System.out.println("cats is "+cats); for (int i = (cats.size()-1); i >=0; i--) System.out.println(cats.get(i)); int[][] matrix = { { 1, 2, 3, 4}, {40, 50, 60, 70}, {7, 8 , 9, 10}}; System.out.println("int[][] matrix = { { 1, 2, 3, 4}, {40, 50, 60, 70}, {7, 8 , 9, 10}}"); for (int k=0; k < matrix.length; k++) { for (int m=0; m < matrix[k].length; m++) { System.out.printf("%d\t",matrix[k][m]); } System.out.println(""); } // practice with multi dimensional ArrayList ArrayList< ArrayList > pets = new ArrayList<>(); ///ArrayList apet = new ArrayList<>(); // right idea, wrong way to do it for (int k=0; k < matrix.length; k++) { pets.add(k, new ArrayList<>()); // initialize each row to an empty ArrayList //System.out.println("at top of k loop with k = "+k+" pets is "+pets); for (int m=0; m < matrix[k].length; m++) { pets.get(k).add(m, matrix[k][m]); //System.out.printf("%d\t",matrix[k][m]); System.out.printf("at pets.get("+k+").get("+m+") %d\n",pets.get(k).get(m)); } //System.out.println("at bottom of k loop with k = "+k+" pets is "+pets); } System.out.println("pets is "+pets); // adds everything // quotes alternate approach String[] quotes = {"these aren't the droids you're looking for", "the cat and the dog", "hop on pop", "may the force be with you", "a three letter word - the", "lathe is a tool to plane wood"}; System.out.println("\nInitial Quote: \n"); for (int i = 0; i < quotes.length; i++) { System.out.println(quotes[i]); } final int MAX = 10; String[] words; Scanner qLine = new Scanner(System.in); String wd; String newQuote = ""; for (int i = 0; i < quotes.length; i++) { qLine = new Scanner(quotes[i]); newQuote = ""; while (qLine.hasNext()) { wd = qLine.next(); if (wd.equalsIgnoreCase("the")) { newQuote = newQuote +(newQuote.length() == 0 ?"":" ") +"any"; } else { newQuote = newQuote + (newQuote.length() == 0 ?"":" ")+wd; } } quotes[i] = newQuote; } System.out.println("\nQuote results: \n"); for (int i = 0; i < quotes.length; i++) { System.out.println(quotes[i]); } // quotes alternate 2 int index = -1; for (int j = 0; j < quotes.length; j++) { for (int i = 0; i < quotes[j].length(); i++) { index = -1; //System.out.println("(quotes[j].indexOf(\" any \",i) = "+(quotes[j].indexOf(" any ",i))); //System.out.println("(quotes[j].indexOf(\"any \",i) = "+(quotes[j].indexOf("any ",i))); //System.out.println("(quotes[j].indexOf(\" any\",i) = "+(quotes[j].indexOf(" any",i))+ // " quotes[j].length() is "+quotes[j].length()); if (((index = quotes[j].indexOf("any ",i)) > -1) && (index == 0)) { quotes[j] = quotes[j].replace("any ","THE "); i = index; } if (((index = quotes[j].indexOf(" any ",i)) > -1) && (index > 0)) { quotes[j] = quotes[j].replace(" any "," THE "); i = index; } if (((index = quotes[j].indexOf(" any",i)) > -1) && (index == (quotes[j].length() - " any".length()))) { quotes[j] = quotes[j].replace(" any"," THE"); i = index; } else { i = quotes[j].length(); } } } System.out.println("\nQuote after replace: \n"); for (int i = 0; i < quotes.length; i++) { System.out.println(quotes[i]); } } // ArrayList< Class_Type > name // type[] name public static ArrayList catlove(int count, Scanner ifile) { ArrayList fluffies = new ArrayList<>(); int readin; while (ifile.hasNextInt()) { readin = ifile.nextInt(); System.out.println("fluffies next value is "+readin); fluffies.add(readin); System.out.println(fluffies); } return fluffies; } }