-1

I am trying to create a Caesar Shift cipher encryption and decryption program. I need it to be able to accept user input until the user wishes to quit(q), but I end up just getting an extreme repetition of everything.

Here is my Shift Class

import java.util.Scanner;
public class CaesarShift
{
//initialize private string for the alphabet
private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
//public encryption code
public String encryptionMethod(String normText, int caesarShift)
{
    normText = normText.toLowerCase();
    String cipherText = "";
    for (int a = 0; a < normText.length(); a++)
    {
        int charP = ALPHABET.indexOf(normText.charAt(a));
        int shiftValue = (caesarShift + charP) % 26;
        char replaceValue = this.ALPHABET.charAt(shiftValue);
        cipherText += replaceValue;
    }
    return cipherText;
}
public String decryptionMethod(String cipherText,int caesarShift)
{
    cipherText = cipherText.toLowerCase();
    String normText = "";
    for (int a = 0; a < cipherText.length(); a++)
    {
        int charP = this.ALPHABET.indexOf(cipherText.charAt(a));
        int keyValue = (charP - caesarShift) % 26;
        if(keyValue < 0)
        {
            keyValue = this.ALPHABET.length() + keyValue;
       }
       char replaceValue = this.ALPHABET.charAt(keyValue);
       normText += replaceValue;
    }
    return normText;
}

}

Here is my tester method where I tried to instantiate the while loop

import java.util.Scanner;
public class CaesarShiftTester
{
public static void main(String args[])
{
    //import of the scanner method to ask the user for the input they would like
    Scanner in = new Scanner(System.in);
    System.out.println("What is the text you would like to do something with?(q to quit)");
    String normText = in.nextLine();
    System.out.println("What is the Caesar Shift Value?");
    int caesarShift = in.nextInt();
    //new declaration of the CaesarShift class to report back to easily
    CaesarShift shift = new CaesarShift();
    //declare the while method loop
    while(normText != "q")
    {
    //decalre the need properties for the encryption
    String cipherText = shift.encryptionMethod(normText, caesarShift);
    System.out.println("Your normal text is: " + normText);
    System.out.println("Your text after encryption is: " + cipherText);
    String cnormText = shift.decryptionMethod(cipherText, caesarShift);
    System.out.println("Your encrypted text is: " + cipherText);
    System.out.println("Your decrypte text is: " + cnormText);
}
}

}

  • 1
    @Reimeus Believe it or not, the string comparison is not the only problem here. He also never prompts for any user input inside the loop. This is the bigger problem IMHO. – Tim Biegeleisen Mar 22 '16 at 14:25
  • 1
    The debugger will get you there quicker. It looks like you're assigning `normText` only once. Furthermore comparing objects (and therefore strings) with `!=` does not do what you think it does. – Martin Cowie Mar 22 '16 at 14:26

2 Answers2

3
while(normText != "q")

here is your problem. The == and != operators are used to compare references, not values.

change it to:

while(!q.equals(normText))
Stultuske
  • 9,296
  • 1
  • 25
  • 37
3

You never prompt for any user input inside your while loop, hence it is spinning forever. Add the user input check inside the loop:

while (!normText.equals("q")) {
    String cipherText = shift.encryptionMethod(normText, caesarShift);
    System.out.println("Your normal text is: " + normText);
    System.out.println("Your text after encryption is: " + cipherText);
    String cnormText = shift.decryptionMethod(cipherText, caesarShift);
    System.out.println("Your encrypted text is: " + cipherText);
    System.out.println("Your decrypte text is: " + cnormText);

    System.out.println("What is the text you would like to do something with?(q to quit)");
    normText = in.nextLine();
}

As @Stultuske mentioned in his answer, you also were trying to compare the value of a String using the != operator, inside of using String.equals(). But not checking for user input inside the while loop is a bigger problem.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360