2017-11-14 18 views
-3

ユーザーが指定したファイルからテキストを読み込むプログラムをコーディングしようとしていますが、暗号化し、最後に暗号化されたフレーズを別のファイルに書き出します。また、暗号化されたテキストを読み取り、復号化する逆の操作をしたいが、一度に1ステップずつ進めたい。読み込みファイルを暗号化しようとしたときにStringIndexOutOfBoundsExceptionが発生し、暗号化されたフレーズを別のファイルに書き込む

package CaeserCipher1; 

import java.util.Scanner; 
import java.io.*; 

public class CaeserCipher1 { 
public static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWYZ"; 

//Method to pull the plaintext of a file, followed by how far you want to shift plaintext to the right. 
public static String encrypt(String plainText, int shiftKey) { 
    String cipherText =""; 
    for (int i = 0; i < plainText.length(); i++) { 
     int charPosition = ALPHABET.indexOf(plainText.charAt(i)); 
     int keyVal = (shiftKey + charPosition) % 26; 
     char replaceVal = ALPHABET.charAt(keyVal); 
     cipherText += replaceVal; 
    } 
    return cipherText; 
} 
    //Method to pull the encrypted text of a file, followed by how far you want to shift the text to the left. 
    public static String decrypt(String cipherText, int shiftKey) 
    { 
     String plainText = ""; 
     for (int i = 0; i < cipherText.length(); i++) 
     { 
      int charPosition = ALPHABET.indexOf(cipherText.charAt(i)); 
      int keyVal = (charPosition - shiftKey) % 26; 
      if (keyVal < 0) 
      { 
       keyVal = ALPHABET.length() + keyVal; 
      } 
      char replaceVal = ALPHABET.charAt(keyVal); 
      plainText += replaceVal; 
     } 
     return plainText; 
    } 
    public static void main(String[] args) throws IOException { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Would you like to encrypt or decrypt a file? "); 
     String choice; 
     choice = keyboard.next(); 

     if (choice.equals("encrypt")) { 
      System.out.println("What is the name of the file you would like to encrypt?: "); 
      String readFile = ""; 
      readFile = keyboard.next(); 
      System.out.println("What is the name of the file you would like to write the encrypted phrase to?: "); 
      String writeFile = ""; 
      writeFile = keyboard.next(); 

      FileReader fileReader = 
        new FileReader(readFile); 
      BufferedReader bufferedReader = 
        new BufferedReader(fileReader); 

      FileWriter fileWriter = 
        new FileWriter(writeFile); 
      BufferedWriter bufferedWriter = 
        new BufferedWriter(fileWriter); 

      String message = ""; 
      while ((message = bufferedReader.readLine()) !=" ") { 
       bufferedWriter.write(encrypt(message, 8)); 
       bufferedWriter.newLine(); 
      } 
     } 
    } 
} 

私は受信エラー:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String 
index out of range: 25 
    at java.lang.String.charAt(Unknown Source) 
    at CaeserCipher1.CaeserCipher1.encrypt(CaeserCipher1.java:15) 
    at CaeserCipher1.CaeserCipher1.main(CaeserCipher1.java:63) 

私は他の人々の問題を読み取ることにより、境界例外のうちの背後にある一般的な考え方を理解し、それ以外の問題に答えを読んでから、私は見つけることができません私自身のコードで問題が発生します。私は< = <の代わりにifループに使用する他の人々の問題を読んだことがあります。人々が部分文字列形式などを使用しようとしているものを見たことがあります。

助けてください。

編集:読み込み元のファイルには大文字しか使用されないため、アルファベットには大文字のみが使用されます。

+0

keyVal> = 0 && keyVal

+0

隣接する重複する文字を削除する際に[StringIndexOutOfBounds]の重複があります(https://stackoverflow.com/questions/ 41477037/stringindexofbounds-when-removing-adjacent-duplicate-letters) – bated

答えて

2

XALPHABETに追加しています。質問を投稿する前にコードを注意深く読んでください...

あなたのコードでは、ALPHABETの文字以外の文字も処理しないので、Hの8のシフト)を正しく復号化しない暗号文に変換する。

+0

最初の点では十分です。 2番目の場合 - 読み込まれるファイルは大文字のみを使用しますが、それはそれを指定しないと私のせいです。 – Saint

関連する問題