2013-02-20 54 views
1
import java.io.IOException; 
import java.util.Random; 
import java.util.Scanner; 
public class Test{ 

public static void main(String[] Args){ 
    Test ts= new Test(); 
    ts.interphace(); 
} 
public void interphace(){ 
    char option = 0 ; 
    Test ts= new Test(); 

    System.out.println("Type E for encryption and D for decryption (In caps)"); 
    try { 
     option = (char)System.in.read(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    switch(option){ 
    case 'E': 
     ts.encrypt(); 
     break; 
    case 'D': 
     ts.decrypt(); 
     break; 
    default: 
     System.out.println("Option Invalid"); 
    } 
} 
public void encrypt(){ 
//Gives the pcode 
    Test ts= new Test(); 
    System.out.println("Encrypt method initiated"); 
    System.out.println("Please enter the String to be encrypted"); 
    Scanner sc = new Scanner(System.in); 
    String toben = sc.next(); 
    System.out.println("Your String: "+ toben); 
    Random pcodegen = new Random(); 
    int pcode = pcodegen.nextInt(64); 
    System.out.println("Your pcode is: "+pcode); 
    char unen[]=toben.toCharArray(); 
    int unenint[]= new int[unen.length]; 
    char en[]= new char[unen.length]; 
    for(int i = 0;i<=unen.length;i++){ 
     unenint[i]=ts.CharToInt(unen[i]); 
    } 
    for(int i = 0;i<=unen.length;i++){ 
     if(unenint[i]>64) 
      unenint[i]-=pcode; 
     else unenint[i]+=pcode; 
    } 
    for(int i = 0;i<=unen.length;i++){ 
     en[i]=ts.IntToChar(unenint[i]); 
    } 

    String encrypted = new String(en); 
    System.out.println("Encrypted String: "+encrypted); 
} 
public void decrypt(){ 
//Dont forget to enter the pcode  
} 

public int CharToInt(char ch){ 
    int s = (int)ch; 
    return s; 
} 

public char IntToChar(int i){ 
    char ch = (char)i; 
    return ch; 
} 
} 

會發生什麼是在選擇該選項後,加密方法被激活。之後它會要求你輸入字符串。 將數組unen中的字符轉換爲unenint(所以我可以用ASCII形式操作它們)時會出現問題,它會給我那個錯誤。 我知道代碼不是很好,因爲我是新手。 請緊急給我解決方案。 在此先感謝。如何解決此錯誤:java.lang.ArrayIndexOutOfBoundsException:4

+2

在所有的在加密方法循環,​​改變從'<='的條件到'<' – 2013-02-20 06:23:03

回答

1

在所有的for循環中,通過包含=來循環迭代過多。嘗試使用這種策略:

for (int i = 0; i < unen.length; i++) { 

請記住,數組索引0,所以如果你有長度10的陣列,它通過9包容有0可用的指標。 =for循環中有效,您將超過可用的索引。

+0

感謝您的解釋。 – NobleSiks 2013-02-20 09:43:18

0

你的循環

for(int i = 0;i<=unen.length;i++){ 

for(int i = 0;i<=unen.length;i++){ 

應該使用<代替<=

0

java.lang.ArrayIndexOutOfBoundsException: 44位置值是找不到的,這樣你就可以改變你的for循環:

for(int i = 0;i<unen.length;i++) //siutable for your situation 

或者

for(int i = 1;i<=unen.length;i++) 
1

變化

for(int i = 0;i<=unen.length;i++){ 

for(int i = 0;i<unen.length;i++){  

到處

1

試試這個

for(int i = 0;i<=unen.length-1;i++) 

數組索引總是從0開始,所以如果u有4個字符在unen這意味着unen最大的指數是3,你可以在這裏看到

[0,1,2,3] 

我建議使用

for(int i = 0;i<=unen.length-1;i++)

,因爲如果ü嘗試

for(int i = 0;i<unen.length;i++)

這意味着在接觸最大指數時,algortihm會再檢查一次,並浪費你的處理器,如果超出,它將停止。

的意思而如果u使用

for(int i = 1;i<=unen.length;i++)

你會在情況下失去了0指數也許ü需要它

+0

非常感謝。有效。很好的解釋。我忘了數組索引從0開始。* facepalm * – NobleSiks 2013-02-20 09:42:40

+0

我歡迎兄弟,所以請加1 ROFTL :)) – shadrachJabonir 2013-02-20 14:47:46