Caesar Cipher (Java)

Java Caesar Cipher

One of the earlier programs I wrote in Python was a Caesar Cipher (later updating it to add a GUI). As part of my recent Java learning I have created a Caesar Cipher in Java. If you want to read more about Caesar Ciphers then please check out:

Java Caesar Cipher

I have placed the Java file for this project ( on my GitHub:

/* GeekTechStuff Caesar Cipher */

import java.util.Scanner;

public class cipher {

    public static void main(String[] args){
        System.out.print("Caesar Cipher Number: ");
        Scanner input = new Scanner(;
        int cipherNumber = input.nextInt(); //takes user input
        System.out.println(replaceAlpha(cipherNumber)); //runs replaceAlpha which does the cipher work


    public static String normalizeText(){
    /* Remove spaces and non-letters from text */
    System.out.print("Enter text to cipher: ");
    Scanner input = new Scanner(;
    String userInput = input.nextLine(); //takes user input
    String inputUpper = userInput.toUpperCase(); //converts input to UPPER case
    String removeSpaces = inputUpper.replaceAll("\\s+",""); //removes any spaces
    String removeNonLetters = removeSpaces.replaceAll("[^a-zA-Z]",""); //removes any non-letter characters
    String normlizedText = removeNonLetters;

    public static String replaceAlpha(int cipher_shift){
        String textToUse = normalizeText(); //runs normalizeText to take text input
        String newString ="";
        int lengthOfText = textToUse.length();
        for(int i=0;i<lengthOfText;i++){
            char letter = textToUse.charAt(i);
            int postition = letters.indexOf(i);
            char replacedLetters = letters.charAt(i+cipher_shift);
            newString = newString+replacedLetters;


One thought on “Caesar Cipher (Java)

Comments are closed.