Advertisement
MarShar

Untitled

Apr 9th, 2020
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.03 KB | None | 0 0
  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import java.lang.Number;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9.  
  10. /**
  11.  *
  12.  * CSCU9T4 Java strings and files exercise.
  13.  *
  14.  */
  15. public class FilesInOut {
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         // Replace this with statements to set the file name (input) and file name (output).
  19.         // Initially it will be easier to hardcode suitable file names.
  20.  
  21.         // Set up a new Scanner to read the input file.
  22.         // Processing line by line would be sensible here.
  23.         // Initially, echo the text to System.out to check you are reading correctly.
  24.         // Then add code to modify the text to the output format.
  25.  
  26.         // Set up a new PrintWriter to write the output file.
  27.         // Add suitable code into the above processing (because you need to do this line by line also.
  28.         // That is, read a line, write a line, loop.
  29.  
  30.         // Finally, add code to read the filenames as arguments from the command line.
  31.          
  32. //         String inFileName = "input.txt";
  33. //         String outFileName = "output.txt";
  34.          
  35.          String cmdInFileName = args[0];
  36.          String cmdOutFileName = args[1];
  37.          
  38.          boolean uppercase = false;
  39.        
  40.          for (int i = 0; i < args.length; i++) {
  41.              if (args[i].equals("-u")) {
  42.                  uppercase = true;
  43.              }
  44.          }
  45.          readWriteFile(cmdInFileName, cmdOutFileName, uppercase);
  46.          
  47.     } // main
  48.    
  49.     public static void readWriteFile(String inFileName, String outFileName, boolean uppercase) {
  50.        
  51.         try {
  52.             File input = new File("C:\\Users\\marks\\eclipse-workspace\\T4Prac4\\" + inFileName); // Set input file
  53.             File output = new File("C:\\Users\\marks\\eclipse-workspace\\T4Prac4\\"+ outFileName); // Set output file
  54.            
  55.             Scanner scan = new Scanner(input); // Declare Scanner
  56.             PrintWriter printer = new PrintWriter(output); // Declare printWriter
  57.             while(scan.hasNextLine()) { // Loop while text exists in file
  58.                 String s = scan.nextLine(); // Scan next line
  59.                 String[] splitStrings = s.split(" ", 4);
  60.                 String finalOutput = "";
  61.                
  62.                 if (splitStrings.length < 4) {
  63.                     if (uppercase) {
  64.                         finalOutput = formattedOutputUpperCase3(splitStrings[0], splitStrings[1], splitStrings[2]);
  65.                     }
  66.                     else finalOutput = formattedOutput3(splitStrings[0], splitStrings[1], splitStrings[2]);
  67.                 }
  68.                 else {
  69.                     if (uppercase) {
  70.                         finalOutput = formattedOutputUpperCase(splitStrings[0], splitStrings[1], splitStrings[2], splitStrings[3]);
  71.                     }
  72.                     else finalOutput = formattedOutput(splitStrings[0], splitStrings[1], splitStrings[2], splitStrings[3]);
  73.                 }
  74.                 System.out.println(finalOutput); // Print out to console
  75.                 printer.write(finalOutput + "\n"); // Format
  76.                
  77.                
  78.             }
  79.             scan.close();
  80.             printer.close();
  81.         }
  82.         catch (FileNotFoundException e) {
  83.             System.err.println("File not found");
  84.         }
  85.        
  86.     } // readFile
  87.    
  88.     public static String capitalise(String str) {
  89.         if(str == null || str.isEmpty()) { // Check there is a string
  90.             return str;
  91.         }
  92.  
  93.         return str.substring(0, 1).toUpperCase() + str.substring(1); // Convert first letter of string to Uppercase
  94.     }
  95.    
  96.     public static String formatDate(String date) {
  97.         SimpleDateFormat inputDate = new SimpleDateFormat("ddmmyyyy"); // Inputed format
  98.         SimpleDateFormat newFormat = new SimpleDateFormat("dd/mm/yyyy");// New format
  99.         String reformattedStr = "";
  100.        
  101.         try {
  102.             reformattedStr = newFormat.format(inputDate.parse(date)); // Format the String
  103.         }
  104.         catch (ParseException e) {
  105.             e.printStackTrace();
  106.         }
  107.        
  108.         return reformattedStr; // return
  109.        
  110.     }
  111.    
  112.     public static String formattedOutput(String firstWord,  String middleName, String secondWord, String date) {
  113.         String finalOutput = capitalise(firstWord) + " " + capitalise(middleName) + ". " + capitalise(secondWord) + "\t\t " + formatDate(date);
  114.        
  115.         return finalOutput;
  116.     }
  117.    
  118.     public static String formattedOutputUpperCase(String firstWord, String middleName, String secondWord, String date) {
  119.         String finalOutput = firstWord.toUpperCase() + " " + middleName.toUpperCase() + ". " + secondWord.toUpperCase() + "\t\t " + formatDate(date);
  120.        
  121.         return finalOutput;
  122.     }
  123.    
  124.     public static String formattedOutput3(String firstWord, String secondWord, String date) {
  125.         String finalOutput = capitalise(firstWord) + " " + capitalise(secondWord) + "\t\t " + formatDate(date);
  126.        
  127.         return finalOutput;
  128.     }
  129.    
  130.     public static String formattedOutputUpperCase3(String firstWord, String secondWord, String date) {
  131.         String finalOutput = firstWord.toUpperCase() + " " + secondWord.toUpperCase() + "\t\t " + formatDate(date);
  132.        
  133.         return finalOutput;
  134.     }
  135. } // FilesInOut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement