Advertisement
allerost

obscure

Jan 19th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.70 KB | None | 0 0
  1. package ar223ni_assign4;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. public class ObscureLovecraft {
  7.     public static void main(String[] args) throws IOException {
  8.         //Add all my scanners and the filepaths
  9.         Scanner sc = new Scanner(System.in);
  10.         File testFile = new File("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
  11.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
  12.         int input = 0;
  13.         //Test if the file lovecraft.txt exists, otherwise throw exception
  14.         try{
  15.             if(!testFile.exists()){
  16.                 throw new IOException("File does not exist");
  17.             }
  18.         }
  19.         catch(IOException e){
  20.             System.out.println(e.getMessage());
  21.         }
  22.         //See what the user wants to do, pick a method depending on the input
  23.         System.out.println("Obscure");
  24.         System.out.println("========");
  25.         System.out.println("1. Make obscure");
  26.         System.out.println("2. Make readable");
  27.         System.out.println("3. print obscure");
  28.         System.out.println("4. print readable");
  29.         System.out.println("0. EXIT");
  30.         input = sc.nextInt();
  31.         try{
  32.             if(input < 0 || input > 4){
  33.                 throw new IllegalArgumentException("That is not an option.");
  34.             }
  35.         }
  36.         catch(IllegalArgumentException e){
  37.             System.out.println(e.getMessage());
  38.             input = sc.nextInt();
  39.         }
  40.         while(input != 0){ //if it is 0 then the code stops running
  41.             System.out.println("===> " + input);
  42.             if(input == 1){ //call method one --> makeObscure()
  43.                 makeObscure();
  44.             }
  45.             else if(input == 2){ //call method two --> makeReadable()
  46.                 makeReadable();
  47.             }
  48.             else if(input == 3){ //call method three --> printObscure()
  49.                 printObscure();
  50.             }
  51.             else if(input == 4){ //call method four --> printReadable()
  52.                 printReadable();
  53.             }
  54.             System.out.println("Done");
  55.             System.out.println(" ");
  56.             System.out.println("Obscure");
  57.             System.out.println("========");
  58.             System.out.println("1. Make obscure");
  59.             System.out.println("2. Make readable");
  60.             System.out.println("3. print obscure");
  61.             System.out.println("4. print readable");
  62.             System.out.println("0. EXIT");
  63.             input = sc.nextInt();
  64.         }
  65.         System.out.println("Bye Bye!!");
  66.     }
  67.  
  68.     public static void makeObscure() throws IOException {
  69.         File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  70.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
  71.         Scanner readFile = new Scanner(fileReader);
  72.         String currentRow = "";
  73.         String newRow = "";
  74.         int ascii = 0;
  75.         char newchar;
  76.  
  77.         try{
  78.             if(!obscure.exists()){
  79.                 obscure.createNewFile();
  80.             }else{
  81.                 //File already exists
  82.             }
  83.         } catch (IOException e) {
  84.             e.printStackTrace();
  85.         }
  86.  
  87.  
  88.         PrintWriter printWriter = new PrintWriter(obscure);
  89.         while(readFile.hasNextLine()){
  90.             currentRow = readFile.nextLine();
  91.             if(currentRow.isEmpty()){
  92.                 printWriter.print("\n");
  93.             }
  94.             else {
  95.                 for (int i = 0; i < currentRow.length(); i++) {
  96.                     if(i == currentRow.length() - 1){
  97.                         printWriter.println();
  98.                     }
  99.                     else{
  100.                         ascii = (int) currentRow.charAt(i);
  101.                         ascii += 3;
  102.                         newchar = (char) ascii;
  103.                         printWriter.print(newchar);
  104.                     }
  105.                 }
  106.             }
  107.  
  108.         }
  109.     }
  110.  
  111.     public static void makeReadable() throws IOException {
  112.         File readable = new File("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
  113.         File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  114.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  115.         boolean read = false;
  116.         Scanner readFile = new Scanner(fileReader);
  117.         String currentRow = "";
  118.         String newRow = "";
  119.         int ascii = 0;
  120.         char newChar;
  121.         try{
  122.             if(!obscure.exists()){
  123.                 //Keep it at false
  124.             }
  125.             else{
  126.                 //Turn it to true and starting reading the file
  127.                 if(!readable.exists()){
  128.                     readable.createNewFile();
  129.                 }
  130.                 else {
  131.                     read = true;
  132.                 }
  133.             }
  134.         } catch (Exception e) {
  135.             e.printStackTrace();
  136.         }
  137.  
  138.         PrintWriter printWriter = new PrintWriter(readable);
  139.  
  140.         while(read && readFile.hasNextLine()){
  141.             currentRow = readFile.nextLine();
  142.             if(currentRow.isEmpty()){
  143.                 printWriter.print("\n");
  144.             }
  145.             else {
  146.                 for (int i = 0; i < currentRow.length(); i++) {
  147.                     if(i == currentRow.length() -1){
  148.                         printWriter.println();
  149.                     }
  150.                     else{
  151.                         ascii = (int) currentRow.charAt(i);
  152.                         ascii -= 3;
  153.                         newChar = (char) ascii;
  154.                         printWriter.print(newChar);
  155.                     }
  156.                 }
  157.             }
  158.         }
  159.     }
  160.  
  161.     public static void printObscure() throws FileNotFoundException {
  162.         File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  163.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  164.         Scanner readFile = new Scanner(fileReader);
  165.         String currentRow = "";
  166.         //throw exception????
  167.         while(readFile.hasNextLine()){
  168.             currentRow = readFile.nextLine();
  169.             System.out.println(currentRow);
  170.         }
  171.     }
  172.  
  173.     public static void printReadable() throws FileNotFoundException {
  174.         File readble = new File("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
  175.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
  176.         Scanner readFile = new Scanner(fileReader);
  177.         String currentRow = "";
  178.         //throw exception?????
  179.         while(readFile.hasNextLine()){
  180.             currentRow = readFile.nextLine();
  181.             System.out.println(currentRow);
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement