Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: java ceaser cipher stanford lectures

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.

     
     
    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;
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java ceaser cipher stanford lectures

    My code doesnt work correctly
    Can you explain? Show what the code does print out and also show what you think it should output.
    Last edited by Norm; July 24th, 2011 at 10:16 AM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    forms (July 24th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  6. The Following User Says Thank You to Norm For This Useful Post:

    forms (July 24th, 2011)

  7. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  9. The Following User Says Thank You to Norm For This Useful Post:

    forms (July 24th, 2011)

  10. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  12. The Following User Says Thank You to Norm For This Useful Post:

    forms (July 24th, 2011)

  13. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by forms; July 24th, 2011 at 11:20 AM.

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    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.

  15. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java ceaser cipher stanford lectures

    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?

  17. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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
    _______________

  18. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  19. The Following User Says Thank You to Norm For This Useful Post:

    forms (July 24th, 2011)

  20. #15
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: java ceaser cipher stanford lectures

    thanks for your help

  21. #16
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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?

     
    	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;

  22. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java ceaser cipher stanford lectures

    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.

  23. The Following User Says Thank You to Norm For This Useful Post:

    forms (July 27th, 2011)

Similar Threads

  1. mod 95 vigninere cipher
    By fortune2k in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 5th, 2010, 09:43 PM
  2. Creating Block cipher without the use of inbuilt java funcs
    By fortune2k in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 15th, 2010, 09:43 AM
  3. creating a ceaser cipher
    By fortune2k in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 15th, 2010, 09:36 AM
  4. Error in Lectures notes(please help)
    By Pulse_Irl in forum AWT / Java Swing
    Replies: 1
    Last Post: October 12th, 2010, 12:48 PM
  5. TCP chat attaching cipher is not running
    By Koren3 in forum Java Networking
    Replies: 4
    Last Post: May 19th, 2009, 02:52 AM