allerost

loveCraft

Jan 19th, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.03 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.         //Make all my files, readers and writers
  70.         File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  71.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
  72.         Scanner readFile = new Scanner(fileReader);
  73.         String currentRow = "";
  74.         String newRow = "";
  75.         int ascii = 0;
  76.         char newchar;
  77.  
  78.         //Check if the file exists
  79.         try{
  80.             if(!obscure.exists()){
  81.                 obscure.createNewFile();
  82.             }else{
  83.                 //File already exists
  84.             }
  85.         } catch (IOException e) {
  86.             e.printStackTrace();
  87.         }
  88.  
  89.         //Make the printer
  90.         PrintWriter printWriter = new PrintWriter(obscure);
  91.         while(readFile.hasNextLine()){
  92.             currentRow = readFile.nextLine();
  93.             if(currentRow.isEmpty()){ //If the row is empty, skip and print the next
  94.                 printWriter.print("\n");
  95.             }
  96.             else {
  97.                 for (int i = 0; i < currentRow.length(); i++) {
  98.                     if(i == currentRow.length() - 1){ //If you are on the right on a row, use println to go down one row
  99.                         printWriter.println();
  100.                     }
  101.                     else{
  102.                         ascii = (int) currentRow.charAt(i); //Read a char
  103.                         ascii += 3; //Add 3 to the ascii value(making it 3 letter bigger)
  104.                         newchar = (char) ascii; //turn it back into a char
  105.                         printWriter.print(newchar); //print the new char
  106.                     }
  107.                 }
  108.             }
  109.  
  110.         }
  111.     }
  112.  
  113.     public static void makeReadable() throws IOException {
  114.         File readable = new File("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
  115.         File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  116.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  117.         boolean read = false;
  118.         Scanner readFile = new Scanner(fileReader);
  119.         String currentRow = "";
  120.         String newRow = "";
  121.         int ascii = 0;
  122.         char newChar;
  123.         try{
  124.             if(!obscure.exists()){
  125.                 //Keep it at false
  126.             }
  127.             else{
  128.                 //Turn it to true and starting reading the file
  129.                 if(!readable.exists()){
  130.                     readable.createNewFile();
  131.                 }
  132.                 else {
  133.                     read = true;
  134.                 }
  135.             }
  136.         } catch (Exception e) {
  137.             e.printStackTrace();
  138.         }
  139.  
  140.         PrintWriter printWriter = new PrintWriter(readable);
  141.  
  142.         while(read && readFile.hasNextLine()){
  143.             currentRow = readFile.nextLine();
  144.             if(currentRow.isEmpty()){
  145.                 printWriter.print("\n");
  146.             }
  147.             else {
  148.                 for (int i = 0; i < currentRow.length(); i++) {
  149.                     if(i == currentRow.length() -1){
  150.                         printWriter.println();
  151.                     }
  152.                     else{
  153.                         ascii = (int) currentRow.charAt(i);
  154.                         ascii -= 3;
  155.                         newChar = (char) ascii;
  156.                         printWriter.print(newChar);
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.     }
  162.  
  163.     public static void printObscure() throws FileNotFoundException {
  164.         File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  165.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\obscure.txt");
  166.         Scanner readFile = new Scanner(fileReader);
  167.         String currentRow = "";
  168.         //throw exception????
  169.         while(readFile.hasNextLine()){
  170.             currentRow = readFile.nextLine();
  171.             System.out.println(currentRow);
  172.         }
  173.     }
  174.  
  175.     public static void printReadable() throws FileNotFoundException {
  176.         File readble = new File("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
  177.         FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
  178.         Scanner readFile = new Scanner(fileReader);
  179.         String currentRow = "";
  180.         //throw exception?????
  181.         while(readFile.hasNextLine()){
  182.             currentRow = readFile.nextLine();
  183.             System.out.println(currentRow);
  184.         }
  185.     }
  186. }
Add Comment
Please, Sign In to add comment