/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code23apr19; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code23Apr19 { /** * @param args the command line arguments */ public static void main(String[] args) { final int MAXWDS = 10; String[] words = new String[MAXWDS]; Scanner inKybd = new Scanner(System.in); //System.out.println("Please enter the name of your data file of words: "); //String fileName = inKybd.nextLine(); File nameFile = new File("inputWords23Apr"); Scanner nameDataFile = new Scanner(System.in); try { nameDataFile = new Scanner(nameFile); } catch (FileNotFoundException fnfe) { System.out.println("No data file found"); } String testName = ""; while (nameDataFile.hasNext()) { testName = nameDataFile.next(); System.out.println("The word \""+testName+"\" is "+whatIs(testName)); } // find palindromes with loops // single words, no spaces, all lower case // check charAt(0) with charAt( .length -1) // if true, loop with substring(1, .length()-1) String orig = "able was I ere I saw elba"; System.out.println("Testing to see if \""+orig+"\" is a palindrome"); boolean maybePalindrome = true; while ((orig.length()>1) && maybePalindrome) { if (orig.charAt(0) == orig.charAt(orig.length()-1)) { orig = orig.substring(1,orig.length()-1); System.out.println(orig); } else { maybePalindrome = false; } } System.out.println("\nString was "+(maybePalindrome?"":"not ")+"a palindrome\n"); // alt approach, reverse string and compare with original orig = "Able was I ere I saw Elba"; String reverse = ""; for (int m = 0; m < orig.length(); m++) //reverse[orig.length() - 1 - m] = orig[m]; reverse = ""+reverse+orig.charAt(orig.length() - 1 - m); System.out.println("\nOrig is *"+orig+"* Revers = *"+reverse+"*"); System.out.println(orig + " is "+( orig.equalsIgnoreCase(reverse) ? "" : "not " )+"a palindrome"); System.out.println(""); String[] shNames = {"Batman", "Spiderman", "Aquagirl","Wonder Woman", "Atomic Robo", "Antman", "Kiteman", "Invisible Woman", "Aquaman", "Elastigirl", "Batgirl", "Martian Manhunter"}; // find all the names that contain the substring "girl" System.out.println(""); for (int i = 0; i < shNames.length; i++) System.out.println(shNames[i]); System.out.println("\nSuperhero names containing \"man\""); for (int i = 0; i < shNames.length; i++) { if ((shNames[i].toLowerCase().contains("man")) && !(shNames[i].toLowerCase().contains("woman"))) { System.out.println(shNames[i]); } } System.out.println(""); for (int i = 0; i < shNames.length; i++) shNames[i] = shNames[i].concat(" stinks"); for (int i = 0; i < shNames.length; i++) System.out.println(shNames[i]); } public static boolean whatIs(String str) { if((str.length() == 0) || (str.length() == 1)) { return true; } if(str.charAt(0) == str.charAt(str.length()-1)) { return whatIs(str.substring(1, str.length()-1)); } else return false; } }