user3855020
user3855020

Reputation: 1

Caesar Cipher Help ASCII Loop

I am trying to create a caesar cipher and I have the input as a hard code in the program. When I run the program it works but everything that should be a "Y" is a "?". I cannot figure out how to loop the ASCII around so when it is less than 65 it adds 26 or loops back to 90. Here is the code I have so far. The shift is -3 to decipher it.

import java.util.Scanner;


public class Caesar {

public static void main(String[] args)
{
  Scanner in = new Scanner(System.in);

  System.out.print("Enter the shift value");

  int shiftValue = in.nextInt();




String ciphertext = "WKHQHZRUNLQJDQGFBEHUVHFXULWBGHSDUWPHQWDWURVHVWDWHF ROOHJHRIIHUVDFHUWLILFDWHSURJUDPLQLQIRUPDWLRQVHFXULWBDW DOOIHGHUDOWUDLQLQJOHYHOVWKHSURJUDPFHUWLILHVWKHVWXGH QWVVDWLVIBLQJSURJUDPUHTXLUHPHQWVDUHWUDLQHGWRWKHIHGH UDOQVWLVVLFQVVLVWDQGDUGVIRULQIRUPDWLRQVBVWHPVVHFXUL WBSURIHVVLRQDOVGHVLJQDWHGDSSURYLQJDXWKRULWLHVLQIRUPD WLRQVBVWHPVHFXULWBRIILFHUVVBVWHPVFHUWLILHUVDQGULVNDQ DOBVWUHVSHFWLYHOB";
//System.out.print(ciphertext);
System.out.println();
//I take each character, convert it to its ascii value, subtract 3,
//then convert back to a character



for(int i=0; i <= 48; i++) {        
System.out.print((char)((int)ciphertext.charAt(i)+shiftValue));
}
System.out.println();
for(int i=49; i <= 102; i++) {
    System.out.print((char)((int)ciphertext.charAt(i)+shiftValue));
    }
System.out.println();
for(int i=103; i < 152; i++) {
    System.out.print((char)((int)ciphertext.charAt(i)+shiftValue));
    }
System.out.println();
for(int i=152; i < 200; i++) {
    System.out.print((char)((int)ciphertext.charAt(i)+shiftValue));
    }
System.out.println();
for(int i=200; i < 248; i++) {
    System.out.print((char)((int)ciphertext.charAt(i)+shiftValue));
    }
System.out.println();
for(int i=248; i < 307; i++) {
    System.out.print((char)((int)ciphertext.charAt(i)+shiftValue));
    }
System.out.println();
for(int i=307; i < 348; i++) {
    System.out.print((char)((int)ciphertext.charAt(i)+shiftValue));
    }
System.out.println();
for(int i=348; i < ciphertext.length(); i++) {
    System.out.print((char)((int)ciphertext.charAt(i)+shiftValue));
    }
//System.out.print((char)((int)ciphertext.charAt(1)+shiftValue));
//System.out.print((char)((int)ciphertext.charAt(2)+shiftValue));
//System.out.print((char)((int)ciphertext.charAt(3)+shiftValue));
//System.out.print((char)((int)ciphertext.charAt(4)+shiftValue));
//System.out.print((char)((int)ciphertext.charAt(5)+shiftValue));
//System.out.println();

}

} // end main
// end class Assign1

Upvotes: 0

Views: 2996

Answers (2)

Matthew Jacobs
Matthew Jacobs

Reputation: 167

First of all, I recommend taking this:

(char)((int)ciphertext.charAt(i)+shiftValue)

and making it into a separate method: [1]

public char newChar(char oldChar, int shiftValue) {
    return (char)((int)ciphertext.charAt(i)+shiftValue);
}

Now that we have this method we can ask what's going on. If you call newChar('B', -3), the desired output is Y and what you're actually getting is ?.

public char newChar(char oldChar, int shiftValue) {
    int value = (int)ciphertext.charAt(i);
    value = value + shiftValue;
    // do something here
    return (char) value;
}

This is likely a school assignment so I won't fill in all the details for you. I hope this gives you a clear path of how to proceed.

[1] Any time you're writing the same thing multiple times it's a good candidate for refactoring.

Upvotes: 1

vandale
vandale

Reputation: 3650

you can check if the resulting value is out side the range of capital letters and adjust it approriately:

char cipher(char input,int shift){
    shift%=26;            //incase the shift is really big
    input+=shift;         //first shift the input charachter
    if(input>'Z')         //if its over 'Z' shift it down into range
        input-=26;
    else if(input <'A')   //if its below 'A' shift it up into range
        input+=26;
    return input;        
}

Also It would be easier to use a String[] for your input instead of counting where each lne ends

Upvotes: 1

Related Questions