Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Assignment 8 // Problem 1
- * For UPEI Computer Science 1910 Fall 2018
- *
- * @author Josh Theriault
- * @version 11/25/2018
- */
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- public class Problem1
- {
- /**
- * Opens the specified file by fileName for reading and returns a Scanner for it
- * @param fileName the file name
- * @return Scanner object handling the opened file
- */
- private static Scanner openFile(String fileName) throws FileNotFoundException
- {
- return new Scanner(new File(fileName));
- }
- /**
- * Opens the specified file by fileName for writing and returns a PrintWriter for it
- * @param fileName the file name
- * @return PrintWriter object handling the opened file
- */
- private static PrintWriter writeFile(String fileName) throws FileNotFoundException
- {
- return new PrintWriter(fileName);
- }
- /**
- * Returns an ArrayList<String> of all the words found in the Scanner object
- * @param input Scanner object that handles the input to scan
- * @return an ArrayList<String> of all the words
- */
- private static ArrayList<String> findWords(Scanner input)
- {
- ArrayList<String> words = new ArrayList<String>();
- input.useDelimiter("[\\s,;:]+");
- while (input.hasNext())
- {
- String word = input.next();
- words.add(word.replaceAll("[^a-zA-Z0-9]", ""));
- }
- return words;
- }
- /**
- * Returns an ArrayList<String> of all the palindromes found in the words array
- * @param words an ArrayList<String> of words
- * @return an ArrayList<String> of palindrome words
- */
- private static ArrayList<String> findPalindromes(ArrayList<String> words)
- {
- ArrayList<String> palindromes = new ArrayList<String>();
- for (int i = 0; i < words.size(); i++)
- {
- String word = words.get(i);
- String wordBackwards = "";
- if (word.length() > 1)
- {
- for (int j = word.length() - 1; j >= 0; j--)
- {
- wordBackwards += word.charAt(j);
- }
- if (word.equalsIgnoreCase(wordBackwards))
- {
- palindromes.add(word);
- }
- }
- }
- return palindromes;
- }
- /**
- * Returns an ArrayList<String> of all the long words found in the words array
- * @param words an ArrayList<String> of words
- * @return an ArrayList<String> of long words
- */
- private static ArrayList<String> findLongWords(ArrayList<String> words)
- {
- ArrayList<String> longWords = new ArrayList<String>();
- for (int i = 0; i < words.size(); i++)
- {
- String word = words.get(i);
- if (word.length() >= 9)
- {
- longWords.add(word);
- }
- }
- return longWords;
- }
- /**
- * Main entry point
- */
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- System.out.print("Input file name: ");
- String inputFileName = in.nextLine();
- System.out.print("Output file name: ");
- String outputFileName = in.nextLine();
- Scanner inFile;
- PrintWriter outFile;
- //Attempt to open input file
- try
- {
- inFile = openFile(inputFileName);
- }
- catch (FileNotFoundException e)
- {
- System.out.println("ERROR: Could not open file \""+inputFileName+"\" for reading");
- return;
- }
- //Attempt to open output file
- try
- {
- outFile = writeFile(outputFileName);
- }
- catch (FileNotFoundException e)
- {
- System.out.println("ERROR: Could not open file \""+inputFileName+"\" for writing");
- return;
- }
- //Find words, palindromes, and long words
- ArrayList<String> words = findWords(inFile);
- ArrayList<String> palindromes = findPalindromes(words);
- ArrayList<String> longWords = findLongWords(words);
- //Output results
- outFile.printf("Palindromes: %s%n", palindromes.toString().replaceAll("[,\\[\\]]+", ""));
- outFile.printf("Long words: %s%n", longWords.toString().replaceAll("[,\\[\\]]+", ""));
- System.out.println("File " + outputFileName + " has been created.");
- inFile.close();
- outFile.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement