Printing encoded information in File written to directory

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 572 Views - Last Post: 04 October 2012 - 01:22 PM Rate Topic: -----

#1 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Printing encoded information in File written to directory

Posted 04 October 2012 - 06:13 AM

The following code imports a File from my java directory, encodes and decodes the file, then writes a new copy in the same directory as either decodeFile or encodeFile. Everything works fine, except the new files do not contain any information. They appear as blank files, when they should contain the encrypted data. I know there is some minor detail I am missing, and I will probably smack myself in the head when someone points it out, but it's not jumping out at me at the moment. The problem likely lies in the encodeFile and decodeFile methods.


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Scanner;

public class Decryption {
	private static char[] alpha;
	private static Scanner input;

	public static void main(String[] args) throws IOException {
		alpha = new char[26];
		loadData();
		input = new Scanner(System.in);
		while (true) {
			displayMenu();
			int choice = input.nextInt();
			input.nextLine();
			if (choice == 1) {
				String phrase = getPhrase("Please enter a phrase to decode");
				decodeMessage(phrase);
			} else if (choice == 2) {
				String phrase = getPhrase("Please enter a phrase to encode");
				encodeMessage(phrase);
			} else if (choice == 3) {
				displayAlphabet();
			} else if (choice == 4) {
				decodeFile();
			} else if (choice == 5) {
				encodeFile();
			} else if (choice == 6) {
				System.out.println("Goodbye!");
				break;
			}
		}
	}

	private static void encodeMessage(String phrase) {
		phrase = phrase.toUpperCase();
		for (int i = 0; i < phrase.length(); i++) {
			if (Character.isLetter(phrase.charAt(i))) {
				for (int j = 0; j < alpha.length; j++) {
					if (phrase.charAt(i) == alpha[j]) {
						if (j == alpha.length - 1) {
							System.out.print(alpha[0]);
						} else {
							System.out.print(alpha[j + 1]);
						}
					}
				}
			} else {
				System.out.print(phrase.charAt(i));
			}
		}
		System.out.println();
	}
	
	private static void encodeFile() {
		
		try{
			File myFile = new File("C:/java/test.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			FileWriter writer = new FileWriter("C:/java/encodedFile.txt");
			writer.write(line);
			
			while ((line = reader.readLine()) != null) {
				encodeMessage(line);
				System.out.println(writer);
			}
			reader.close();
			
		} catch( Exception ex) {
			ex.printStackTrace();
		}
		
	}
	
	private static void decodeFile() {
		try{
			File myFile = new File("C:/java/test.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			while ((line = reader.readLine()) != null) {
				decodeMessage(line);
				FileWriter writer = new FileWriter("C:/java/decodedFile.txt");
				writer.write(line);
				System.out.println(writer);
			}
			reader.close();
			
		} catch( Exception ex) {
			ex.printStackTrace();
		}
		
	}
		
	private static void decodeMessage(String phrase) {
		phrase = phrase.toUpperCase();
		for (int i = 0; i < phrase.length(); i++) {
			if (Character.isLetter(phrase.charAt(i))) {
				for (int j = 0; j < alpha.length; j++) {
					if (phrase.charAt(i) == alpha[j]) {
						if (j == 0) {
							System.out.print(alpha[alpha.length - 1]);
						} else {
							System.out.print(alpha[j - 1]);
						}
					}
				}
			} else {
				System.out.print(phrase.charAt(i));
			}
		}
		System.out.println();
	}

	private static String getPhrase(String message) {
		System.out.println(message);
		String phrase = input.nextLine();
		return phrase;
	}

	private static void displayMenu() {
		System.out.println("Caesar Cipher");
		System.out.println("1. Decode a message.");
		System.out.println("2. Encode a message.");
		System.out.println("3. Display the alphabet.");
		System.out.println("4. Decode the imported text file.");
		System.out.println("5. Encode the imported text file.");
		System.out.println("6. Exit the program.");
		System.out.println("Please enter a number (1-6):");
	}

	private static void loadData() {
		for (int i = 0; i < alpha.length; i++) {
			alpha[i] = (char) (i + 65);
		}
	}

	private static void displayAlphabet() {
		for (int i = 0; i < alpha.length; i++) {
			System.out.print(alpha[i]);
		}
		System.out.println();
	}
}



You can also see that I tried two different approaches with FileWriter

Is This A Good Question/Topic? 0
  • +

Replies To: Printing encoded information in File written to directory

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 06:30 AM

your encodeMessage method only prints the encoded message in your console, it doesn't put anything in a file.

System.out.println(writer); will just print out the writer object, and not actually do anything.

You should change your code so that encodeMessage returns a String with the encoded content. Your writer should then write this encoded message

Remember to close your writer
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 06:39 AM

Not that important, but you should decrease the time you have the file open for read/write

I wrote a skeleton of how you could reduce the time having the files open
It is up to you to implement it :)
This way you wont be having an open file when doing the encoding

String content = readAllLines("C:/java/test.txt");
String encodedMessage = encode(content);
writeMessage("C:/java/encodedFile.txt", encodedMessage);

public String readAllLines(String filePath) {
	StringBuilder lines = new StringBuilder();
	//Read file and append to StringBuilder
	return lines.toString();
}

public void writeMessage(String filePath, String message) {

}

//Message can be multi line
public String encode(String message) {

}


Was This Post Helpful? 1
  • +
  • -

#4 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 06:45 AM

Ahh, I was focusing on the wrong method. I'll try that then.
Was This Post Helpful? 0
  • +
  • -

#5 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 07:12 AM

View PostCasiOo, on 04 October 2012 - 06:30 AM, said:

your encodeMessage method only prints the encoded message in your console, it doesn't put anything in a file.

System.out.println(writer); will just print out the writer object, and not actually do anything.

You should change your code so that encodeMessage returns a String with the encoded content. Your writer should then write this encoded message

Remember to close your writer


No, that's not making any sense actually. Maybe if I create an array instead?
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 07:56 AM

What doesn't make any sense to you?
Was This Post Helpful? 1
  • +
  • -

#7 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 08:17 AM

The problem is that your code is actually not printing anything to the files. As an example, look at lines 73-74 from your code:

encodeMessage(line);
System.out.println(writer);



The first line is encoding the message and printing it to the console (trace through the encodeMessage to validate this is correct), and then you are printing the writer object to the console.

There are two ways to solve this problem:

  • Pass the writer to the encodeMessage method and have it print to that instead of the console
  • Have the encodeMessage method return the encoded string and write it to the file in the calling method


Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#8 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 08:43 AM

Sorry Casio, I hope I didn't offend you. rfs02 helped clarify it though. Thanks guys.
Was This Post Helpful? 0
  • +
  • -

#9 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 09:56 AM

So I closed the writer as such:


private static void encodeFile() {
		
		try{
			File myFile = new File("C:/java/test.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			while ((line = reader.readLine()) != null) {
				encodeMessage(line);
				FileWriter writer = new FileWriter("C:/java/encodedFile.txt");
				writer.write(line);
				writer.close();
				System.out.println(writer);
			}
			reader.close();
			
		} catch( Exception ex) {
			ex.printStackTrace();
		}
		
	}




Now, do I pass the writer to the encodeMessage paramater, as such?

private static void encodeMessage(String phrase, FileWriter writer) {


Was This Post Helpful? 0
  • +
  • -

#10 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 10:08 AM

when I closed the writer, the file will print the original text in the new .txt document. Of course, it's still not encoded, so I understand why I have to pass it to encodeMessage.
Was This Post Helpful? 0
  • +
  • -

#11 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 10:44 AM

I would rather have the encodeMessage method to return a String and not having it do any IO at all
Was This Post Helpful? 0
  • +
  • -

#12 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 10:48 AM

Yeah, I was thinking of that too. The method should only do one thing, right? So, the encodeFile method is fine like it is. I simply have to alter encodeMessage. However, how can I return a String if it is set to void? Am I misunderstanding what you mean by return?
Was This Post Helpful? 0
  • +
  • -

#13 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 11:42 AM

you should set the return type of the method to string.

One more thing to note in your code is that you are opening and closing the file in every iteration of the loop. Make sure you run your program with an input text file that has more than one line to see what happens.
Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • Joined: 05-April 11

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 11:45 AM

View Postiburres, on 04 October 2012 - 05:48 PM, said:

Yeah, I was thinking of that too. The method should only do one thing, right? So, the encodeFile method is fine like it is. I simply have to alter encodeMessage. However, how can I return a String if it is set to void? Am I misunderstanding what you mean by return?


You should change the return type from void to String
Take a look at the skeleton I made earlier, it does exactly that :)

Also I don't think you should initialize (or close) your FileWriter inside your while-loop, that will be very inefficient!
You will only ever get 1 line in your text file, because new FileWriter("C:/java/encodedFile.txt") will not append to the existing file, but instead start from scratch.
If you use new FileWriter("C:/java/encodedFile.txt", true); then it will append to the file
Was This Post Helpful? 0
  • +
  • -

#15 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Printing encoded information in File written to directory

Posted 04 October 2012 - 12:04 PM

I'm pretty much ready to just throw my computer out the window. Idon't know why it's not sinking in, probably because I've been sitting at my computer typing this program and trying to solve this problem since 7 am. Here is what I have, based on the advice you have given me, though I know it is not correct.


private static String encodeMessage(String phrase) {
		String line = new String();
		line = line.toString();
		phrase = phrase.toUpperCase();
		for (int i = 0; i < phrase.length(); i++) {
			if (Character.isLetter(phrase.charAt(i))) {
				for (int j = 0; j < alpha.length; j++) {
					if (phrase.charAt(i) == alpha[j]) {
						if (j == alpha.length - 1) {
							System.out.print(alpha[0]);
						} else {
							System.out.print(alpha[j + 1]);
						}
					}
				}
			} else {
				System.out.print(phrase.charAt(i));
			}
		}
		System.out.println();
		return line;
	}
	
	private static void encodeFile() {
		
		try{
			File myFile = new File("C:/java/test.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			while ((line = reader.readLine()) != null) {
				encodeMessage(line);
				FileWriter writer = new FileWriter("C:/java/encodedFile.txt", true);
				writer.write(line);
				writer.close();
				System.out.println(writer);
			}
			reader.close();
			
			
		} catch( Exception ex) {
			ex.printStackTrace();
		}
	}



The thing is, String line does absolutely nothing, since it has not been assigned the encoded message.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2