在使我的代码正常运行方面存在问题,第一部分可以正常工作,它可以对输入的代码进行加密并打印结果,但是当函数进入我遇到的方法移位字母时,
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 0.
有人在这里挣扎时可以向我解释我的项目哪里出了问题。
代码在下面。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner( System.in );
String encryptedText;
int encryptedNumber;
int length;
System.out.println("Please input text to encrypt");
encryptedText = sc.nextLine();
System.out.println("Enter a number for encryption amount");
encryptedNumber = sc.nextInt();
System.out.println("Enter a length for your segment");
length = sc.nextInt();
System.out.println("Thank you for your input");
String compressedvalue = encryptString(encryptedText,encryptedNumber,length);
System.out.println(" Your encrypted value is:" + compressedvalue);
String decryptedText = ungroupify(compressedvalue);
System.out.println("The decryption algorithm produced: " + decryptedString(decryptedText,encryptedNumber));
System.out.println("Thank you");
normalizeText("" );
}
// Part 1 Normalized Method to convert all letter to uppercase and removing all special characters and placing it into String codey.
public static String normalizeText (String codey){
System.out.println(codey.replaceAll(" ","").replaceAll("[^a-zA-Z ]", "").toUpperCase());
System.out.println(codey);
obify("");
return normalizeText(codey);
}
public static String obify(String input){
String obifiledInput="";
char[] vowels={'A','E','I','O','U','Y'};
for (int i=0;i<input.length();i++){
boolean flagVowelNotHave=true;
for(int j=0;j<vowels.length;j++){
if (input.charAt(i)==vowels[j]){
obifiledInput+=("OB"+input.charAt(i));
flagVowelNotHave=false;
}
}
if(flagVowelNotHave)obifiledInput+=input.charAt(i);
}
ceasarify("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);
return obifiledInput;
}
public static String ceasarify(String a, int n){
String letterIndex = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
String returnString = "";
for (int cnt = 0; cnt < a.length(); cnt++) {
char oLetter = a.charAt(cnt);
int oIndex = letterIndex.indexOf(oLetter);
int nIndex = oIndex + n;
if (nIndex >= 0) {
if (nIndex > 25) {
while (nIndex > 25) { nIndex -= 26; }
}
}
else {
while (nIndex < 0) { nIndex += 26; }
}
returnString = returnString + letterIndex.charAt(nIndex);
}
groupify( "", 26 );
return returnString;
}
public static String groupify( String p, int f){
int newy = p.length();
String segmented = "";
int bigLength = newy % f;
for (int chop = 0; chop < newy; chop += f) {
if (chop + f > newy) {
segmented = segmented + p.substring(chop, newy);
for (int x = 0; x < bigLength; x++) {
segmented = segmented + "x";
}
}
else { segmented = segmented + p.substring(chop, chop + f) + " "; }
}
if (bigLength == 0) { segmented = segmented.substring(0,segmented.length() - 1); }
shiftAlphabet(0);
return segmented;
}
public static String shiftAlphabet(int shift) {
int start = 0;
if (shift < 0) {
start = (int) 'Z' + shift + 1;
} else {
start = 'A' + shift;
}
String result = "";
char currChar = (char) start;
for(; currChar <= 'Z'; ++currChar) {
result = result + currChar;
}
if(result.length() < 26) {
for(currChar = 'A'; result.length() < 26; ++currChar) {
result = result + currChar;
}
}
encryptString("",26,25);
return result;
}
public static String encryptString(String code, int shifty, int big) {
String ntext = normalizeText(code);
String ctext = ceasarify(ntext,shifty);
String gtext = groupify(ctext,2 );
return gtext;
}
private static String decryptedString (String decryptedText, int encryptedNumber) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
encryptedNumber = encryptedNumber * -1; //Reverse the value.
String bigText = "";
for (int i = 0; i < decryptedText.length(); i++) {
int oldText = alphabet.indexOf(decryptedText.charAt(i));
int newText = oldText + encryptedNumber;
// Wrap if longer/shorter than alphabet.
if (newText < 0) { while(newText < 0) { newText += 26; } }
else if (newText > 25) { while(newText > 25) { newText -= 26; } }
bigText = bigText + alphabet.charAt(newText);
}
return bigText;
}
private static String ungroupify (String compressedvalue) {
compressedvalue = compressedvalue.replace("x", ""); //Remove space-filling x's
compressedvalue = compressedvalue.replace(" ", ""); //Remove spaces.
return compressedvalue;
}
}