Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. /**
  2.  * Assignment 8 // Problem 1
  3.  * For UPEI Computer Science 1910 Fall 2018
  4.  *
  5.  * @author Josh Theriault
  6.  * @version 11/25/2018
  7.  */
  8.  
  9. import java.util.Scanner;
  10. import java.util.ArrayList;
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.io.PrintWriter;
  14.  
  15. public class Problem1
  16. {
  17.     /**
  18.      * Opens the specified file by fileName for reading and returns a Scanner for it
  19.      * @param fileName the file name
  20.      * @return Scanner object handling the opened file
  21.      */
  22.     private static Scanner openFile(String fileName) throws FileNotFoundException
  23.     {
  24.         return new Scanner(new File(fileName));
  25.     }
  26.    
  27.     /**
  28.      * Opens the specified file by fileName for writing and returns a PrintWriter for it
  29.      * @param fileName the file name
  30.      * @return PrintWriter object handling the opened file
  31.      */
  32.     private static PrintWriter writeFile(String fileName) throws FileNotFoundException
  33.     {
  34.         return new PrintWriter(fileName);
  35.     }
  36.    
  37.     /**
  38.      * Returns an ArrayList<String> of all the words found in the Scanner object
  39.      * @param input Scanner object that handles the input to scan
  40.      * @return an ArrayList<String> of all the words
  41.      */
  42.     private static ArrayList<String> findWords(Scanner input)
  43.     {
  44.         ArrayList<String> words = new ArrayList<String>();
  45.        
  46.         input.useDelimiter("[\\s,;:]+");
  47.         while (input.hasNext())
  48.         {
  49.             String word = input.next();
  50.             words.add(word.replaceAll("[^a-zA-Z0-9]", ""));
  51.         }
  52.        
  53.         return words;
  54.     }
  55.    
  56.     /**
  57.      * Returns an ArrayList<String> of all the palindromes found in the words array
  58.      * @param words an ArrayList<String> of words
  59.      * @return an ArrayList<String> of palindrome words
  60.      */
  61.     private static ArrayList<String> findPalindromes(ArrayList<String> words)
  62.     {
  63.         ArrayList<String> palindromes = new ArrayList<String>();
  64.        
  65.         for (int i = 0; i < words.size(); i++)
  66.         {
  67.             String word = words.get(i);
  68.             String wordBackwards = "";
  69.             if (word.length() > 1)
  70.             {
  71.                 for (int j = word.length() - 1; j >= 0; j--)
  72.                 {
  73.                     wordBackwards += word.charAt(j);
  74.                 }
  75.                 if (word.equalsIgnoreCase(wordBackwards))
  76.                 {
  77.                     palindromes.add(word);
  78.                 }
  79.             }
  80.         }
  81.        
  82.         return palindromes;
  83.     }
  84.    
  85.     /**
  86.      * Returns an ArrayList<String> of all the long words found in the words array
  87.      * @param words an ArrayList<String> of words
  88.      * @return an ArrayList<String> of long words
  89.      */
  90.     private static ArrayList<String> findLongWords(ArrayList<String> words)
  91.     {
  92.         ArrayList<String> longWords = new ArrayList<String>();
  93.        
  94.         for (int i = 0; i < words.size(); i++)
  95.         {
  96.             String word = words.get(i);
  97.             if (word.length() >= 9)
  98.             {
  99.                 longWords.add(word);
  100.             }
  101.         }
  102.        
  103.         return longWords;
  104.     }
  105.    
  106.     /**
  107.      * Main entry point
  108.      */
  109.     public static void main(String[] args)
  110.     {
  111.         Scanner in = new Scanner(System.in);
  112.        
  113.         System.out.print("Input file name: ");
  114.         String inputFileName = in.nextLine();
  115.        
  116.         System.out.print("Output file name: ");
  117.         String outputFileName = in.nextLine();
  118.        
  119.         Scanner inFile;
  120.         PrintWriter outFile;
  121.        
  122.         //Attempt to open input file
  123.         try
  124.         {
  125.             inFile = openFile(inputFileName);
  126.         }
  127.         catch (FileNotFoundException e)
  128.         {
  129.             System.out.println("ERROR: Could not open file \""+inputFileName+"\" for reading");
  130.             return;
  131.         }
  132.        
  133.         //Attempt to open output file
  134.         try
  135.         {
  136.             outFile = writeFile(outputFileName);
  137.         }
  138.         catch (FileNotFoundException e)
  139.         {
  140.             System.out.println("ERROR: Could not open file \""+inputFileName+"\" for writing");
  141.             return;
  142.         }
  143.        
  144.         //Find words, palindromes, and long words
  145.         ArrayList<String> words = findWords(inFile);
  146.         ArrayList<String> palindromes = findPalindromes(words);
  147.         ArrayList<String> longWords = findLongWords(words);
  148.        
  149.         //Output results
  150.         outFile.printf("Palindromes: %s%n", palindromes.toString().replaceAll("[,\\[\\]]+", ""));
  151.         outFile.printf("Long words: %s%n", longWords.toString().replaceAll("[,\\[\\]]+", ""));
  152.        
  153.         System.out.println("File " + outputFileName + " has been created.");
  154.        
  155.         inFile.close();
  156.         outFile.close();
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement