我是java新手。get java.lang.StringIndexOutOfBoundsException嘗試二進制文本轉換時出錯
我想創建程序來在我的程序中將二進制流轉換爲文本首先我可以給出二進制流的數量作爲輸入,然後我可以給二進制numbers.as輸出我想獲得對應於二進制文本的文本numbers.but但我的錯誤在我的program.below我提到的錯誤和program.thank你。
- 錯誤:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 8 at java.lang.String.substring(String.java:1963) at javaapplication32.JavaApplication32.main(JavaApplication32.java:23)
第23行是int m = Integer.parseInt(s.substring(8*k,(k+1)*8),2);
代碼:
import java.util.Scanner; public class JavaApplication32 { public static void main(String[] args) { // TODO code application logic here String s=""; int j; Scanner a=new Scanner(System.in); int b=a.nextInt(); String arr[]=new String[b]; for(int i=0;i<b;i++){ arr[i]=a.next(); } for(j=0;j<b;j++){ for(int k=0;k<arr[j].length()/8;k++){ int m = Integer.parseInt(s.substring(8*k,(k+1)*8),2); s += (char)(m); } System.out.println(s); s=""; } } }
實施例:
input:
2 011100000111100101110100011010000110111101101110 0110110001110101011011
output:
python lu
[內環的Java string.Substring的StringIndexOutOfBoundsException]的可能的複製(https://stackoverflow.com/questions/19910791/java-string-substring-stringindexoutofboundsexception-inside-loop) – Tom
嘗試在自己的解釋說出你的代碼應該如何工作。你的循環中的條件是基於'arr [j]',但是你正在從's'中「剪切」部分,這也應該存儲你的結果。 – Pshemo
@Tom我試過我最好的。我是編程新手。我無法理解this.but謝謝你的努力。 – Intern