Gxfire
Gxfire

Reputation: 25

Java: Caesar Cipher won't println

The code compiles fine, but it does not print anything in the end. I'm new to coding and have been working on this for hours and am at a wall.

Here is the code:

import java.util.Scanner;
public class caesartwo {
public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    String originalText;
    int shiftValue; 

    //encryption
        System.out.println("What text would you like to encrypt?");
        originalText=keyboard.nextLine();

        //shift value
        System.out.print("\nWhat is the shift value? ");
        shiftValue=keyboard.nextInt();

        //encrypted string
        String encryptedText=encrypt(originalText,shiftValue);

        //print result
        System.out.println("\nThe encrypted text is:\n" + encryptedText);
    }
public static String rotate(String userString, int shiftValue) { 
    String convertedText = "";
    for(int i = 0; i < userString.length(); i++){
    char lowerLetter = userString.charAt(i);

    //uppercase conversion
    char upperLetter = Character.toUpperCase(lowerLetter);
    int charNumber = upperLetter;

    //shift and wrap
    int rotateShift = (charNumber + shiftValue) % 26;
    char shiftLetter = (char) rotateShift;

    //shifted chars
    convertedText += shiftLetter;
    }
  return convertedText;
}
public static String encrypt(String userString, int shiftValue) {
    String encryptedString = rotate(userString , shiftValue);
    return encryptedString;
}
}

Now I have to write some more words because I have too much code text blah blah blah.

Upvotes: 1

Views: 568

Answers (4)

Durandal
Durandal

Reputation: 5663

You've got some blur going on with your variables in your rotate() method. You should extract them and make them instance variables instead of letting each iteration of the loop create new variables. You can do this much simpler. Replace your rotate method with the below:

public static String rotate(String userString, int shiftValue) { 
    String convertedText = "";
    int offset = shiftValue % 26 + 26;
    int j;
    for(int i = 0; i < userString.length(); i++){
        j = (userString.charAt(i) - 'a' +offset) % 26;
        convertedText += (char) (j+'a');
    }
  return convertedText;
}

Tested it and it spits out a value now. Highjacked the algorithm from here: http://rosettacode.org/wiki/Caesar_cipher#Java

Upvotes: 3

jeremyjjbrown
jeremyjjbrown

Reputation: 8009

Your problem is here.

char shiftLetter = (char) rotateShift;

shiftletter never gets assigned a value when I debug through the code. Learn to debug your code and you'll be able to find an offending statement quickly.

Upvotes: 1

tom87416
tom87416

Reputation: 542

It won't print because of this line

int rotateShift = (charNumber + shiftValue) % 26

it will return 0-25 which is not mapped to english character in ASCII

you should do

int rotateShift = (charNumber - (int)('A') + shiftValue) % 26+ (int)('A');

Upvotes: 1

hallfox
hallfox

Reputation: 66

Look at the line rotateShift = (charNumber + shiftValue) % 26;. I understand that you're trying to wrap the ASCII value around so that if you shift 'Z' by one you get 'A'. However, on the ASCII table, 'A' begins at 65. Modding (charNumber + shiftValue) by 26, the answer could only be a number between 0 and 25 inclusive. And if you look at the ASCII table, 0-25 are all special characters that aren't printable like the NULL character and CARRIAGE RETURN. To ensure the desired result, I suggest adding (charNumber + shiftValue) % 26 by 65 so that you start at 'A' and won't exceed 'Z'.

Upvotes: 1

Related Questions