0

This is the code I am working upon. I dont know where I am going wrong.

package mcdcpairwise;
import java.io.*;
import java.util.*;

public class Permutation
{
    public static void main(String[] args)
    {
        String a="000";
        String b="|&";

        for (int i=0; i < a.length(); i++){
            if (i % 2 != 0){
                a = a.substring(0,i-1) + b.substring(0,i-1). + a.substring(i, a.length()) + b.substring(i, b.length());
                System.out.println(a);
            }
        }

    }
}    

The error I am facing is:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2 at java.lang.String.substring(String.java:1967) at mcdcpairwise.Permutation.main(Permutation.java:13)

The output should be :

0|0&0

  • The code you posted produces `&00` as the output for me, which is what it looks like it should... `b` and `|` is never used. – Zephyr Jun 05 '18 at 03:18
  • while using b I was facing errors.So I tried taking & to do – Averty micc Jun 05 '18 at 03:20
  • Possible duplicate of https://stackoverflow.com/questions/537174/putting-char-into-a-java-string-for-each-n-characters#537185 - Check the answers there; they may help. – Zephyr Jun 05 '18 at 03:30
  • I think the first `a.substring(0,i-1)` should be `a.substring(0,i)` – Dawood ibn Kareem Jun 05 '18 at 03:42
  • Welcome to SO. The code posted does not produce the error. You are trying to add `"&"` to the string, so why do you expect the out put to be `0|0|0` ? Also from the output you see that the loop runs once only. – c0der Jun 05 '18 at 03:44
  • @Zephyr that method is working for strings. It says Integers cannot be de-referenced. Is there any method that it would work for Integers? – Averty micc Jun 05 '18 at 04:29
  • Possible duplicate of [Java - StringIndexOutOfBoundsException](https://stackoverflow.com/questions/19670369/java-stringindexoutofboundsexception) – AxelH Jun 05 '18 at 04:51
  • @AxelH [ link ] (https://stackoverflow.com/questions/19670369/java-stringindexoutofboundsexception ) is not what I am looking for – Averty micc Jun 05 '18 at 05:38
  • Nah, but this would help you solved the exception you got at one point :`Exception in thread "main" java.lang.StringIndexOutOfBoundsException` – AxelH Jun 05 '18 at 05:44
  • You need to be more clear here. How do you want to determine which character is to be inserted where? – Zephyr Jun 05 '18 at 05:47

4 Answers4

0

The right code should be a.substring(0,i).

VienNguyen
  • 12
  • 3
  • Just `a.substring(0,i)`? That's a short code to merge two `String` ! See [answer] please. – AxelH Jun 05 '18 at 04:52
0

It isn't clear from your question exactly what your "rules" are for processing this. However, your output seems to simply insert a character between each character of your source a string.

Instead of using a substring, create a separate StringBuilder to add individual characters to. The code below produces the output you are looking for:

String string = "000";
StringBuilder output = new StringBuilder();

for (int i = 0; i < string.length(); i++) {

    // Get current character in the string
    char c = string.charAt(i);

    // Add the current character to the output
    output.append(c);

    // If more characters exist, add the pipe
    if (i != string.length() - 1) {
        output.append("|");
    }
}

System.out.println(output.toString());
Zephyr
  • 9,885
  • 4
  • 28
  • 63
  • But the idea is not to insert `|` between each characters. If `b = "/\\"` this should output `0/0\0` based on the question... – AxelH Jun 05 '18 at 04:55
  • Yeah, that is not at all clear in the question. Nowhere in the question is that explained... – Zephyr Jun 05 '18 at 05:07
  • My question ? Check again ;-) Just a hint, OP name is in a gray rectangle in the comments -> [`Averty micc`](https://stackoverflow.com/users/9894389/averty-micc) – AxelH Jun 05 '18 at 05:12
  • I edited my comment; definitely assumed you were the OP since you claim the question says something it doesn't say. :P – Zephyr Jun 05 '18 at 05:13
  • 1
    @AxelH your actuallly right. I am trying to insert ant type of character into a string. I will specify now. Sorry for the confusions. Any idea how it can be done. – Averty micc Jun 05 '18 at 05:32
0

You can use String.toCharArray to get a char[] from a String. That way we can iterate more easily both String using an index.

String a="000";
String b="|&";

char[] arrayA = a.toCharArray();
char[] arrayB = b.toCharArray();

Then, all we have to do is to merge two array (from Strings) taking one character from both. Adding two conditions (one per array) to prevent any ArrayIndexOutOfBOundsException, we can insure we will merge two arrays.

StringBuilder sb = new StringBuilder();

//Add a char from both array (until we reach on of the limit)
int i = 0;
while( i < arrayA.length && i < arrayB.length){
    sb.append(arrayA[i]).append(arrayB[i]);
    ++i;
}

Then we just need to add the remaining characters using a for loop on both arrays. Only one of those loop will be triggered (or none) since at least one previous condition (i < arrayA.length && i < arrayB.length) is already false.

//Add the rest of `a` if any
for(int j = i; j < arrayA.length; ++j){
    sb.append(arrayA[j]);
}

//Add the rest of `b` if any
for(int j = i; j < arrayB.length; ++j){
    sb.append(arrayB[j]);
}

System.out.println(sb.toString());

0|0&0

AxelH
  • 14,325
  • 2
  • 25
  • 55
0

Here’s a one line solution:

System.out.println((a + b).replaceAll("(?<=.)(?=.{" + (a.length() - 1) + "}(.))|.(?=.{0," + (b.length() - 1) + "}$)", "$1"));

This works with all combinations of non-blank starting strings.

See live demo.

Bohemian
  • 412,405
  • 93
  • 575
  • 722