У меня возникли некоторые проблемы с этим изначально, но здесь я нашел другую помощь. теперь у меня, похоже, проблема с ошибкой ввода-вывода. Я считаю, что у меня есть правильное форматирование ввода.Caesar Shift Cipher, ошибка исключения ввода
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;
}
}
Тогда я метод тестер там, где я, имеющий реальную проблему ошибки ввода исключений
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?");
String normText = in.next();
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();
//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);
}
}
Извините за несколько грязного кода, я, как правило, очистку когда программа выполняется и работает.
Добро пожаловать на переполнение стека. Это хорошая идея, чтобы очистить код, прежде чем вы отправляете вопрос, в идеале сводя его к [mcve]. На данный момент мы знаем, что существует «ошибка исключения ввода», но не где, или что такое сообщение, и т. Д. Я настоятельно призываю вас сократить код до минимального примера, включить ошибку в вопрос и что вы пытались диагностировать. Вы вполне можете обнаружить, что в процессе этого вы решаете проблему ... –