• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encrypt code using Caesar cipher  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to read a text from the text file, convert to lower case letters, count no of characters and do the encryption using the formula y=x+noc. But I can't get it to run other than System.out.println(text); It does not show any error when it compiles but it doesn't show all the other System.out.println.

import java.io.BufferedReader; //Scanner
import java.io.FileReader;



 
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it ever actually stop?
I would expect it to eventually run out of memory.

Though I could be misreading that code:


'text' gains a character everytime you go around that loop, so i will always be less than text.length().
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I inserted one line in the text file, "I love burgers" It should stop right after it reaches alphabet s right?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:I inserted one line in the text file, "I love burgers" It should stop right after it reaches alphabet s right?
No, it won't stop. Please read what DT told you earlier.

I corrected your code tags; use the button rather than trying to write tags by hand. And welcome to the Ranch
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:I inserted one line in the text file, "I love burgers" It should stop right after it reaches alphabet s right?


By the time it finishes with 's' the code has already added the cipher for 'I love burgers' to the end of text...so it continues around the loop.

Try this, and you'll see what I mean:

and keep the rest of the loop the same.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What they're hinting at is that you need to keep your original text intact. Don't add anything to it. Instead, create another string for the cipherText.  Those are the terms in this problem domain: "plain text" is the unencoded string, "cipher text" is the encoded string.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell! I changed plainText to cipherText and now the string prints fine. But the thing it doesn't all shift +13 for example. The first character shift+1, the second character shift+2, the third character shift+3 and so on. And there is characters where the whitespace should be. I don't want that.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured this is because the value of noc changes everytime it loops. So how do I make it take the final value of noc as the constant value. So that each time noc  is being used it only has the final constant value which is 13. Does anyone understand what I'm trying to say?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want the offset to be the number of characters excluding whitespace?
The simplest is count the number of characters first.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. But the if loop already does that right? I mean, the counter increases everytime it meets a character that is not a whitespace.(I think this is the cause of my problem.) Should I remove the if loop?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it increments as it goes.
According to your newer post you want it to use 13 (the number of characters) all the time.
So you need to count the characters first, before going through the individual characters and offsetting them.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:. . .  Should I remove the if loop?
That looks like guessing. You can guess 1000000× and get a correct solution somewhere, or you can think the problem through and get a correct program first time. Turn your computer off, get a pencil and paper and write down what you are planning to do. Draw a diagram of “I like burgers” and show what you want as each letter is encoded.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:Thanks Campbell! I changed plainText to cipherText and now the string prints fine. But the thing it doesn't all shift +13 for example. The first character shift+1, the second character shift+2, the third character shift+3 and so on. And there is characters where the whitespace should be. I don't want that.


Think about the problem you are describing, then look at what these statements in your program do:

Do you see how these two statements directly relate to the problem you are describing?

Also, I can guess what "noc" stands for but you really shouldn't use variable names like that, they are cryptic and they don't make your program clear and expressive. Names like that tend to lead you astray and lead to bugs.

Try writing down a sentence that explains what that variable is for and pick one or two words from that sentence that uniquely and clearly captures the idea of the sentence. That is what your variable should be named.

Just like how the name "cipherText" clearly expresses the purpose of the String that is called that.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I came up with. But the whitespace is being replaced by an alphabet so my output becomes e.g qwertyuiopa instead of q wert tyuiopa.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bug in a program reveals a difference between what you, the programmer, think you are telling the computer to do versus what you actually told the computer to do.

Please explain what you think lines 28 to 37 of your program do. Then we'll at least be on the same page and we can help you realize where you've gone astray.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you would, tell us which particular line(s) of your program will leave space characters unchanged and add them "as is" to your cipherText String.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the way your program is shaping up right now, there will be certain Strings for which your program will produce a cipherText value that appears to be exactly the same as your plaintext, except the cipherText will be all lowercase letters. Edit: That is assuming you fix the bug that makes your program incorrectly handle spaces.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I think it does. Line 28 to 37- well basically, plainText.charAt(0), represents alphabet 'I' in "I love burgers". The if section checks if the next character stored in plainText.charAt(1), plainText.charAt(2) etc is a whitespace or not. If it is a whitespace, then the noOfCharacters counter does not increase. But if it is not a whitespace(an alphabet is actually present) then, the noOfCharacters counter will increase. The for section basically loops the whole thing so that the process will continue until each of the characters in the "I love burgers" line has been read. It reads one character at a time. I don't have any lines that will leave space characters unchanged. I have no idea how to implement it.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now we're getting somewhere. I see your mind is stuck in the nitty gritty details. If I understand what you just explained correctly, that section of code is counting how many non-space characters there are in your plain text input. That's the *intent* or *essence* of what that part of your program does, which is what I was trying to get you to explain.

That's fine, at least were on the same page in that respect. So, after that section of code executes, then noOfCharacters will tell you how many non-space characters there are in your plain text. What do you think will happen if that number is a multiple of 26?

Now, as for your bug with the spaces, how do you think you can tell the computer to do this: "If the character in plaintext is a space, just add it directly to cipherText, otherwise figure out what the Caesar code is for it and add that to cipherText"
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While noOfCharacters is an improvement over the name ”noc” it still falls short of expressing its true purpose in your program.

If you write down an explanation of the Caesar cipher, there should be nothing in there that mentions "number of characters in the plain text". There will be, however, a mention of some kind of " shift" or "offset". I suggest you find a name that conveys that idea clearly in your program and use that name in the appropriate part of your code so that it's easier to see where it comes into play when creating your cipherText.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will print out the original letters. The reason why I inserted %26 in the equation is because the alphabets are supposed to be in a cyclic chain. So once it reaches 'z' it will continue to 'a' all over again. The most direct way to translate what you mentioned would be like this, but it certainly doesn't run.

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to admit, I didn't think, in a Caesar cipher, that the offset had anything to do with the number of characters in the String being ciphered...
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:how do you think you can tell the computer to do this: "If the character in plaintext is a space, just add it directly to cipherText, otherwise figure out what the Caesar code is for it and add that to cipherText"

If you figure out how to say that in Java correctly, come back to this statement and see if it's really what you want the computer to do. You have to remember, the computer doesn't actually think. It just blindly follows the directions you have given it. And it takes those directions very literally. If you say "do this if it's a space otherwise do that other thing" it will do exactly just that. Which means if it sees a punctuation character like a comma or a dash or question mark, it will do that other thing because of course those characters are not spaces.

So you have to think very carefully about how you phrase your commands to the computer because you're always going to get what you asked for, even if it's not what you actually wanted. The computer is like a smartass genie.

Genie: I give you three wishes...

You: I wish I knew what to wish for...

Genie: Very well, now what's your second wish?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:The most direct way to translate what you mentioned would be like this, but it certainly doesn't run.

Why do you say that? What happens when you try it?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:I have to admit, I didn't think, in a Caesar cipher, that the offset had anything to do with the number of characters in the String being ciphered...

It usually doesn't, which is why I pointed it out to OP. The name of the variable actually reveals that bug if you stop your brain from translating what it says to what it really is intended to be. Taken only for what "number of characters" literally means, the code reveals that it's not doing what was intended to be done.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:but it certainly doesn't run.


What happens after you fix the problem on line 3 and try to run your code? The "=" is an assignment, "==" is used to compare two things.

Also, two single quotes without anything between them is NOT how you say "a Space character" in Java.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the list of errors I got.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:The most direct way to translate what you mentioned would be like this...

So, once you fix the few remaining problems that I just pointed out to you, you'll find that "the most direct way to translate" is usually what works. You just have to pull your mind out of the weeds and figure out what you *really* want to say in a high-level, tell-grandma-how-to-do-it kind of way.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:Here's the list of errors I got.

Don't post images if you want to show what errors you get from compiling or running. Just copy/paste the text from that window.

Anyway, I already gave you hints on how to correct those errors:
I wrote:The "=" is an assignment, "==" is used to compare two things.

Also, two single quotes without anything between them is NOT how you say "a Space character" in Java.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:It will print out the original letters. The reason why I inserted %26 in the equation is because the alphabets are supposed to be in a cyclic chain. So once it reaches 'z' it will continue to 'a' all over again.
And before you get all excited about seeing your program actually working the way you want it to, think about what I said earlier about this and the problem that you'll have if the number of non-space characters in your plaintext is 26: you'll get the same text back, except in all lowercase.

So, what value should you use to determine your Caesar cipher that's not what noOfCharacters currently represents in your program? Is it something that you can calculate from the plainText value or is it something that you just specify beforehand or is it something you want the user to specify, maybe as a command line argument to your program? You have to decide which way to go and tell the computer how to do it that way.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my current code. I want to be able to write an equation for int y that will solve the problem.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adreena Williams wrote:This is my current code. I want to be able to write an equation for int y that will solve the problem.

I assume that your code is working now?

I'm not sure what you mean by "write an equation for int y that will solve the problem"... do you mean to say you need to be able to decrypt the cipherText?  If so, then consider this:  If you start with 1 and then add 5 to get 6, what do you need to do to get from 6 and go back to the original number that you started with?
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh okay. Thank you very much for your help. I really appreciate it. I've also added this bits of code to save the output to text file.


 
nadeera rajapakse
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code works now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!