2016-04-29 41 views
-3

問題代碼如下,如果您需要整個主要方法來幫助我,請詢問。該代碼符合但未按預期運行。如果數字超出/大於源文本的最後一個位置(這是用戶輸入的字符串),我試圖使代碼報告返回一個感嘆號,因此無法預定義該長度。異常是'StringIndexOutOfBoundsException'字符串索引超出範圍異常,在if else語句上

TDLR num是一個int,sourcetext是一個字符串,兩者都是輸入。例外:當代碼輸出'!'時代替。

import java.util.Scanner; 

public class Temp { 

    public static void main(String[] args) { 

     Scanner sc; 
     int result, num= 0, end = -2, temp, infolost, count; 
     String word, sourcetext, answer, space= " "; 
     String sourcetext2, temp2; 
     char input, result2, chalost; 
     sc = new Scanner(System.in); 

     System.out.println("please enter sourcetext"); 
     sourcetext = sc.nextLine(); // user inputs source text 
     sourcetext = sourcetext.toLowerCase(); // converts sourcetext into lowercase      
     System.out.print("Would you like to 1 encrypt, or 2 decrypt?"); 
     answer = sc.next(); // user inputs choice 

     if (answer.equals("1")||(answer.equals("encrypt"))) { 

      System.out.println("Please enter at least one word to encrypt"); 
      word = sc.next(); // user inputs one word 

      for (int i= 0; i < word.length(); i++) { 
       temp = sourcetext.indexOf(word.charAt(i)); // uses index to convert char positions int num 
       System.out.print(space + temp + space);   
      } 
      System.out.print(space + end); 
     } 
     else if (answer.equals("2")||(answer.equals("decrypt"))) { 

      System.out.println("Please enter digits, with one space between each. End with -2");     
      while (num > -2) { 
       num = sc.nextInt(); // num to decrypt 
       if (num > -2) {  
        result2 = sourcetext.charAt(num); // num converted into characters 
        System.out.print(result2); 
       } else if (num > sourcetext.length()) { 
        System.out.print("!"); 
       } else if (num<0) { 
        System.out.print("end"); 
       } 
      } 
     } 
    } 
} 
+0

當您嘗試訪問不在字符串範圍內的索引時,會發生完整代碼'StringIndexOutOfBoundsException'。 –

+1

您需要提供完整的代碼,可能還有堆棧跟蹤。 – Areca

+3

您需要學習解釋堆棧跟蹤並使用調試器,而不是要求我們爲您調試代碼。 – Raedwald

回答

0

這可能導致IndexOutOfBounds異常 - 因爲-1-2更大 - 但還是出界...

if (num > -2){  
    result2 = sourcetext.charAt(num); // num converted into characters 
    System.out.print(sourcetext.indexOf(num)); 
} 

編輯:除非用戶輸入是-2 - 第一個if - 陳述將始終運行...您可能需要重新工作那裏的邏輯。編號2:如果num-1sourcetext.charAt(num);導致IndexOutOfBounds。做類似

if(num == -2) { 
    System.out.print("end"); 
} else if (num >= 0 && num < sourcetext.lenght()) { 
    // index ok 
    result2 = sourcetext.charAt(num); // num converted into characters 
    System.out.print(result2); 
} else { 
    // index out of bounds 
    System.out.print("!"); 
} 
+0

您能否以不同的方式說出這一點,我無法理解這一點。我想要第一個if語句總是運行,除非答案是-2? Incase你沒有看到編輯,這個>>> System.out.print(sourcetext.indexOf(num)); <<<是一個錯誤,現在是>>> System.out.print(result2); – Button

+0

檢查我的第二次編輯 – user0815

+0

我太新了,無法提供您的答案,但它的工作!所以主要問題在這裏>> if(num == -2)<<< yes?我會仔細研究代碼,看看我出錯的地方,謝謝你花時間幫忙。 – Button

0

試試這樣說:

int stringLength = sourcetext.length(); 

if (num > stringLength) { 
    System.out.print("!"); 
} 
else if (num<0) { 
    System.out.print("end"); 
} 
+0

不幸的是,這不起作用 – Button

+1

你能提供錯誤嗎?它不應該有一個OutOfBound,因爲你只是比較值。順便說一句:if(answer == 1)||(answer.equals(「encrypt」))){//使用==作爲數字。仍在審查你的代碼,將不勝感激一個錯誤消息。 –

+1

if語句'else if(answer.equals(「2」)||(answer.equals(「decrypt」))){'不是錯誤拋出的地方,它從這裏開始'else if(num> sourcetext。長度()){ System.out.print(「!」);'當我嘗試輸入一個大於字符串sourcetext的最後位置的數字時。 E.g sourcetext是「醜陋的」,這是三個字符長,如果我輸入5錯誤拋出而不是添加感嘆號。 – Button

相關問題