I am new to java.
i want to create program to convert binary stream to text in java.in my program first i can give the number of binary streams as input and then i can give binary numbers.as output i want to get texts corresponding to binary numbers.but i get error in my program.below i mentioned error and program.thank you.
- Error:
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)
23rd line is int m = Integer.parseInt(s.substring(8*k,(k+1)*8),2);
code:
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=""; } } }Example:
input:
2
011100000111100101110100011010000110111101101110
0110110001110101011011
output:
python
lu
arr[j]but you are "cutting" parts fromswhich also should store your result.suseresultString. This will allow you to see that your code is doingresultString.substring(8*k,(k+1)*8), but do you really want to pick new data from result?