0

I was working on a Java coding problem and encountered the following issue.

Input: A String -> "Code"
Output Expected: A string -> CCoCodCode

My Code snippet: (Note: In comments I have written what I expect upon passing the string)

public String stringSplosion(String str) { // string Say 'Code'
    String join = "", values = "";
    String gotIt = "";

    int n = str.length(); // 4
    int size = 0;

    for (int i = n; i >= 1; i--) {
        size = size + n; // 4+3+2+1=10
    }

    String[] result = new String[size];

    for (int i = 0; i < str.length(); i++) {
        values = str.substring(i, i + 1);
        join = join + values;
        result[i] = join;
    }

    for (String s : result) {
        gotIt = gotIt + s;
    }

    return gotIt; // Expected output: CCoCodCode
}

Output I am getting:

CCoCodCodenullnullnullnullnullnullnullnullnullnullnullnull

Why is null getting stored although I have reduced the size and how can I remove it?

NOTE: I need to solve this using arrays. I know it is much easier using List.

5
  • Please learn how to enter code. It's not hard, either indent 4 spaces or use the {} button in the embedded editor. Commented Nov 30, 2017 at 19:30
  • sorry for the inconvenience. I will follow it. Commented Nov 30, 2017 at 19:31
  • size is null because your array seems too big? why not only print the value if it is not null Commented Nov 30, 2017 at 19:41
  • @JohnKane array size is not big. I have used a for loop to exactly get the size of the result I want. Commented Nov 30, 2017 at 19:44
  • appreciate all the answers. understood my mistake in the code. Commented Dec 4, 2017 at 19:29

6 Answers 6

1

If you want to keep the current structure of your code, get rid of the first for loop. And create String[] array = new String[n]

public static String stringSplosion(String str) { // string Say 'Code'
String join = "", values = "";
String gotIt = "";

int n = str.length(); // 4

String[] result = new String[n]; //you want your String array to contain 4 strings

for (int i = 0; i < str.length(); i++) {
    values = str.substring(i, i + 1);
    join = join + values;
    result[i] = join;
}

for (String s : result) {
    gotIt = gotIt + s;
}

return gotIt; // Expected output: CCoCodCode
}
Sign up to request clarification or add additional context in comments.

Comments

1
public class Answer {

public static String answer(String input){

    StringBuilder sb = new StringBuilder(((input.length() + 1) * input.length()) / 2);

    for (int i = 1; i <= input.length(); i++) {
        sb.append(input.substring(0, i));
    }

    return sb.toString();
}

public static void main(String[] args) {
    System.out.println(answer("Code"));
}

}

Comments

1

Below statements are not required:

int size = 0;

    for (int i = n; i >= 1; i--) {
        size = size + n; // 4+3+2+1=10
    }

You just need to change the array size from

String[] result = new String[size];

to

String[] result = new String[n];

for your program to give the expected output.

Comments

1

If I understand ur problem correctly to print the pattern then u can use below code,

public String printPattern(String input){    
//Holds the iteration value by index
int previous=0;
//It holds the result characters
String result=null;
StringBuilder strBuilder=new StringBuilder();
//first loop to iterate only till input string length
for(int i=0;i<input.length();i++){
    //checking iteration lenght with input string length 
    if(previous<input.length()){
        //incrementing iteration for reading characters from input string
        previous++;
        //main loop for previous iteration value check and iterate
        for(int j=0;j<previous;j++){
            //converting string to Character array
            char a []=input.toCharArray();
            //using string builder to build the string from characters
            strBuilder.append((a[j]));
            //setting the value to stringbuilder by converting it in string
            result=strBuilder.toString();

        }

    }
}

return result;

}

Comments

0

Size should be the length of string. Code's length is 4. Code will produce {C, Co, Cod, Code}.

  public String stringSplosion(String str) { // string Say 'Code'
    String join = "", values = "";
    String gotIt = "";

    int n = str.length(); // 4

    String[] result = new String[n];

    for (int i = 0; i < str.length(); i++) {
        values = str.substring(i, i + 1);
        join = join + values;
        result[i] = join;
    }
    System.out.println(Arrays.toString(result));

    for (String s : result) {
        gotIt = gotIt + s;
    }

    return gotIt; // Expected output: CCoCodCode
}

Comments

0
String input = "Code";
String output[] = IntStream.range(0, input.length()+1)
            .mapToObj(i -> input.substring(0, i))
            .toArray(String[]::new);

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.