Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ar223ni_assign4;
- import java.io.*;
- import java.util.Scanner;
- public class ObscureLovecraft {
- public static void main(String[] args) throws IOException {
- //Add all my scanners and the filepaths
- Scanner sc = new Scanner(System.in);
- File testFile = new File("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
- FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
- int input = 0;
- //Test if the file lovecraft.txt exists, otherwise throw exception
- try{
- if(!testFile.exists()){
- throw new IOException("File does not exist");
- }
- }
- catch(IOException e){
- System.out.println(e.getMessage());
- }
- //See what the user wants to do, pick a method depending on the input
- System.out.println("Obscure");
- System.out.println("========");
- System.out.println("1. Make obscure");
- System.out.println("2. Make readable");
- System.out.println("3. print obscure");
- System.out.println("4. print readable");
- System.out.println("0. EXIT");
- input = sc.nextInt();
- try{
- if(input < 0 || input > 4){
- throw new IllegalArgumentException("That is not an option.");
- }
- }
- catch(IllegalArgumentException e){
- System.out.println(e.getMessage());
- input = sc.nextInt();
- }
- while(input != 0){ //if it is 0 then the code stops running
- System.out.println("===> " + input);
- if(input == 1){ //call method one --> makeObscure()
- makeObscure();
- }
- else if(input == 2){ //call method two --> makeReadable()
- makeReadable();
- }
- else if(input == 3){ //call method three --> printObscure()
- printObscure();
- }
- else if(input == 4){ //call method four --> printReadable()
- printReadable();
- }
- System.out.println("Done");
- System.out.println(" ");
- System.out.println("Obscure");
- System.out.println("========");
- System.out.println("1. Make obscure");
- System.out.println("2. Make readable");
- System.out.println("3. print obscure");
- System.out.println("4. print readable");
- System.out.println("0. EXIT");
- input = sc.nextInt();
- }
- System.out.println("Bye Bye!!");
- }
- public static void makeObscure() throws IOException {
- File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
- FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\lovecraft.txt");
- Scanner readFile = new Scanner(fileReader);
- String currentRow = "";
- String newRow = "";
- int ascii = 0;
- char newchar;
- try{
- if(!obscure.exists()){
- obscure.createNewFile();
- }else{
- //File already exists
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- PrintWriter printWriter = new PrintWriter(obscure);
- while(readFile.hasNextLine()){
- currentRow = readFile.nextLine();
- if(currentRow.isEmpty()){
- printWriter.print("\n");
- }
- else {
- for (int i = 0; i < currentRow.length(); i++) {
- if(i == currentRow.length() - 1){
- printWriter.println();
- }
- else{
- ascii = (int) currentRow.charAt(i);
- ascii += 3;
- newchar = (char) ascii;
- printWriter.print(newchar);
- }
- }
- }
- }
- }
- public static void makeReadable() throws IOException {
- File readable = new File("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
- File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
- FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\obscure.txt");
- boolean read = false;
- Scanner readFile = new Scanner(fileReader);
- String currentRow = "";
- String newRow = "";
- int ascii = 0;
- char newChar;
- try{
- if(!obscure.exists()){
- //Keep it at false
- }
- else{
- //Turn it to true and starting reading the file
- if(!readable.exists()){
- readable.createNewFile();
- }
- else {
- read = true;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- PrintWriter printWriter = new PrintWriter(readable);
- while(read && readFile.hasNextLine()){
- currentRow = readFile.nextLine();
- if(currentRow.isEmpty()){
- printWriter.print("\n");
- }
- else {
- for (int i = 0; i < currentRow.length(); i++) {
- if(i == currentRow.length() -1){
- printWriter.println();
- }
- else{
- ascii = (int) currentRow.charAt(i);
- ascii -= 3;
- newChar = (char) ascii;
- printWriter.print(newChar);
- }
- }
- }
- }
- }
- public static void printObscure() throws FileNotFoundException {
- File obscure = new File("C:\\Users\\Greattech\\Downloads\\obscure.txt");
- FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\obscure.txt");
- Scanner readFile = new Scanner(fileReader);
- String currentRow = "";
- //throw exception????
- while(readFile.hasNextLine()){
- currentRow = readFile.nextLine();
- System.out.println(currentRow);
- }
- }
- public static void printReadable() throws FileNotFoundException {
- File readble = new File("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
- FileReader fileReader = new FileReader("C:\\Users\\Greattech\\Downloads\\readableLovecraft.txt");
- Scanner readFile = new Scanner(fileReader);
- String currentRow = "";
- //throw exception?????
- while(readFile.hasNextLine()){
- currentRow = readFile.nextLine();
- System.out.println(currentRow);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement