java ceaser cipher stanford lectures
Hi,
i am working through some java stanford tutorials on youtube and i copied out some code from ep 13 from the lecture but cant get it to work.
My code doesnt work correctly. It just prints out the system.print.ln text.
I also included the line ceaser s = new ceaser(); and then wrote s.encrypt to call the method but i dont understand why i needed to do this. They didnt do this in the code online but the code only runs when i include these changes.
Thanks for your help.
Code :
also included the line ceaser s = new ceaser(); and then wrote s.encrypt to call the method but i dont understand why i needed to do this. They didnt do this in the code online but the code only runs when i include these changes.
Code:
import java.util.Scanner;
public class ceaser {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter a ceaser cipher: ");
int key = input.nextInt();
System.out.println("Enter word to encode: ");
String word = input.nextLine();
ceaser s = new ceaser();
String ciphertext = s.encrypt(word, key);
System.out.println("Encoded text: " + ciphertext);
String newword = s.encrypt(ciphertext, -key);
System.out.println("new word: " + newword);
}
private String encrypt(String str, int key){
if(key<0){
key = 26 - (-key%26);
}
String result = "";
for(int i=0; i<str.length(); i++){
char ch = str.charAt(i);
result +=encryptChar(ch, key);
}
return result;
}
private char encryptChar(char ch, int key){
if(Character.isUpperCase(ch)){
return( (char)('A' + ((ch - 'A' + key) % 26)) );
}
return ch;
}
}
Re: java ceaser cipher stanford lectures
Quote:
My code doesnt work correctly
Can you explain? Show what the code does print out and also show what you think it should output.
Re: java ceaser cipher stanford lectures
the output is below. I typed 2.
____________________
Enter a ceaser cipher:
2
Enter word to encode:
Encoded text:
new word:
_________________
The program will ask for a number, and then shift the alphabet by this number. I then enter a word, and it will output the word in the code.
Eg. If i enter 2, then i enter the word CAT, it will shift the letter by 2 and output ECV
Re: java ceaser cipher stanford lectures
Why isn't there a word to encode entered? Your posted console contents does not show a word being entered.
If nothing goes in, then I would think that nothing would come out.
Try it and enter a word and show what happens.
BTW Caesar was the name of a great Roman general.
Re: java ceaser cipher stanford lectures
It doesnt let me enter a word.
It just lets me enter a digit, then it outputs the last two lines.
Im not really sure why this doesnt work as i copied it exactly from the youtube video
I noticed i also spelt Caesars name wrong. :s
Re: java ceaser cipher stanford lectures
Your problem is with the way the Scanner class works. The nextInt() method reads the next word as a number from the input and leaves the endline from Enter in Scanner's buffer. The nextLine() call returns that endline as an empty String.
Try this for now: type both the number and the word to encrypt on the same line before pressing Enter:
2 CAT
That will give nextLine() the CAT from the buffer.
The fix to your program is to add a call to nextLine() to read the empty line BEFORE you ask for the word.
Re: java ceaser cipher stanford lectures
You are right when i enter the number and word on the same line it will output the encoded word
So what should i add to change this?
Thanks for your help by the way.
Re: java ceaser cipher stanford lectures
The fix to your program is to add a call to nextLine() to read the empty line BEFORE you ask for the word.
Re: java ceaser cipher stanford lectures
Thanks for that it worked.
Also why did i need to write ceaser s = new ceaser(); and write s.encrypt?
I know this is a stupid question but i dont really get it. When i write static in the methods instead it works fine.
Re: java ceaser cipher stanford lectures
What did you want the Caesar class to do for you?
Did it have a method that would do that?
Quote:
When i write static in the methods instead it works fine.
Yes, that can work for this specific very small example.
When your programs get larger and when there can be more than one instance of a class, having static variables will be a source of problems. An instance of a class usually needs to have its own variable values. A static variable is shared by all instances and can only have one value for all instances vs a non static variable allowing each instance of the class to have its own value.
Re: java ceaser cipher stanford lectures
That makes sense, thanks for explaining that.
Also when i comment out these two lines from the main method the program works fine. Can you see why these lines of code are included in the program?
String newword = s.encrypt(ciphertext, -key);
System.out.println("new word: " + newword);
Thanks
Re: java ceaser cipher stanford lectures
Quote:
Can you see why these lines of code are included in the program?
Look at the output.
Do you see any relationship between the output of these two lines of code and the input to the program?
Re: java ceaser cipher stanford lectures
I thought these two lines of code were needed when a user inputs a negative number but when i comment out these two lines and input a negative value it works fine.
With these two lines included it just repeats the word that was input into the program. I cant see why these two lines are needed.
Here is some example output:
_______________
Enter a ceaser cipher: 2
Enter word to encode: CAT
Encoded text: ECV
new word: CAT
_______________
Enter a ceaser cipher: -2
Enter word to encode: AAA
Encoded text: YYY
new word: AAA
_______________
Re: java ceaser cipher stanford lectures
Do you see how the algorithm works? The original input is encrypted and then decrypted back to the original input.
Re: java ceaser cipher stanford lectures
Re: java ceaser cipher stanford lectures
Hi, i had another question.
from the code extract below why cant i keep the parameters in the encrypt method as
private String encrypt(String word, int key)
Why do i have to use a new variable called str? I have kept the key variable the same why not the variable word?
Code :
String ciphertext = s.encrypt(word, key);
System.out.println("Encoded text: " + ciphertext);
String newword = s.encrypt(ciphertext, -key);
System.out.println("new word: " + newword);
}
private String encrypt(String str, int key){
if(key<0){
key = 26 - (-key%26);
}
String result = "";
for(int i=0; i<str.length(); i++){
char ch = str.charAt(i);
result +=encryptChar(ch, key);
}
return result;
Re: java ceaser cipher stanford lectures
Quote:
hy cant i keep the parameters in the encrypt method as
private String encrypt(String word, int key)
Why do i have to use a new variable called str? I have kept the key variable the same why not the variable word?
I'm not sure I understand what you are asking.
You can give just about any name you want to the parameters received by a method:
private String encrypt(String theWordToWorkOn, int theKeyThatWillBeUsed)
It is usually better and less confusing if the variable names local to a method are unique to that method and NOT the same as class variables. If they have the same names, the class variables are "shadowed" and not used. This can be a problem if you forget which variable you are using. You could think you are setting a class variable but are really working with a local one. When the method exits, the class variable is unchanged.
Unique names prevents this problem.