-2

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

5
  • Possible duplicate of java string.Substring StringIndexOutOfBoundsException inside loop Commented Jul 23, 2017 at 15:20
  • Try to explain in your own words how your code should work. Condition in your loop is based on arr[j] but you are "cutting" parts from s which also should store your result. Commented Jul 23, 2017 at 15:26
  • @Tom i tried my best.i am new to programming.i cant understand this.but thank for your effort. Commented Jul 23, 2017 at 15:27
  • @Pshemo you will get idea about my program when u see my example.i am new to programming.help me to solve this. Commented Jul 23, 2017 at 15:29
  • 1
    Let me rephrase my advice. Imagine you are explaining your program to someone else (this is called rubber duck debugging). This way you can notice things you want to do, but see that some part of your code doesn't actually follow what you want. For now best advice I can give you is to rename your variables using full names to describe their purpose. For instance instead of s use resultString. This will allow you to see that your code is doing resultString.substring(8*k,(k+1)*8), but do you really want to pick new data from result? Commented Jul 23, 2017 at 15:35

1 Answer 1

1

At this line :

String s="";

You initialize your string s with a 0-length string, and you use it in your loop without assigning it :

for(int k=0;k<arr[j].length()/8;k++){
    int m = Integer.parseInt(s.substring(8*k,(k+1)*8),2); // It crashes here because 's' still have a length of 0, and you ask the substring method to get the substring between indexes 0 and 8
    s += (char)(m);
}

What i think you wanted to do is more like this :

for(int k=0;k<arr[j].length()/8;k++){
    int m = Integer.parseInt(arr[j].substring(8*k,(k+1)*8),2); // here, with arr[j], you use your input
    s += (char)(m);
}

And with this input :

2
011100000111100101110100011010000110111101101110
0110110001110101011011

It outputs this :

python
lu
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.