I'm trying here to segregate o's and 1's in a java String and print the result as such 0's will print at first and then 1's will print here's my code:
int len = scanner.nextInt();
String num = scanner.nextLine();
StringBuilder result = new StringBuilder("");
for(int i = 0;i < len-1;i++){
if(num.charAt(i) == '0'){
result.append("0");
}
}
for(int i = 0;i < len-1;i++){
if(num.charAt(i) == '1'){
result.append("1");
}
}
System.out.print(result);
input should be like this:
5
11010
and the output should be:
00111
but in the above I'm getting error as
java.lang.StringIndexOutOfBoundsException: String index out of range: