-7
import java.util.*;

public class Main {

public static void main(String[] args) {


    Scanner scan1 = new Scanner(System.in);
    int test = scan1.nextInt();
    String[] sArr = new String[test];
    sArr[0] = "";
    // String s = "";
    //  StringBuilder sB = new StringBuilder(s);
    int y = 0;

    while (test > 0) {
        char c;
        test--;


        Scanner scan2 = new Scanner(System.in);
        Scanner scan3 = new Scanner(System.in);

        int n = scan2.nextInt();
        String str = scan3.nextLine();
        String[] nSplit = str.split("(?<=\\G.)");


        int[] x = new int[n];


        for (int i = 0; i < n * 4 - 3; i += 4) {
            nSplit[i] += nSplit[i + 2];
            nSplit[i + 1] += nSplit[i + 3];
        }


        for (int i = 0, j = 0; i < x.length; i++, j += 4) {
            x[i] = Integer.parseInt(nSplit[j]) + Integer.parseInt(nSplit[j + 1]);
            c = (char) x[i];

            sArr[test] += c;

        }


    }


    for (int i = sArr.length-1; i >-1; i--) {

        System.out.println(sArr[i]);
    }

}
}

input:

5

1

1234

1

2345

1

3456

1

4567

1

5678

output:

null%

null;

nullQ

nullg

} //end

where did these null come from? how to get rid of it? why is there not null next to the last element?

if null means a blank at the selected array index then what it means in this case?

Rat95
  • 1
  • 1
  • If `sArr[test]` is null and you add a string to it, then you get a string with `null` at the start. – khelwood Aug 15 '20 at 18:36
  • You should use only one scanner based on System.in. It won't help you with this problem, but it could be a source of future bugs. – NomadMaker Aug 15 '20 at 18:37
  • Have you tried debugging it yourself? –  Aug 15 '20 at 18:37
  • thx khelwood, NomadMaker how to use only one scanner for this? – Rat95 Aug 15 '20 at 18:44
  • What is the purpose using more than one scanner ? You could freely use it during your entire run. – Traycho Ivanov Aug 15 '20 at 18:59
  • Why do you create additional scanners in loop, and two of them? Is that you way around problem described at [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045)? If yes then after `scan1.nextInt()` you can add `scan1.skip("\\R")` to consume line separator sequence which is causing empty string to be returned at `nextLine()`. – Pshemo Aug 15 '20 at 19:04
  • Just use scan1 and don't create any more scanners. Why did you create the other scanners? Is there a use that I don't know about? – NomadMaker Aug 15 '20 at 21:42

1 Answers1

0

Basically what is happening is you are creating an array of Strings. And you are only initialising the first element of the array to "" sArr[0] = "";

That is why every other element but the first element has a null preceding it.

So this statement sArr[test] += c; is going to add a null value for all non initialised strings in your array of Strings.

I would suggest instead of sArr[0] = ""; you can do this Arrays.fill(sArr , "") It initialises all values of the strings in the array to ""

Arrays is a class found under utils which you are importing.

GCode21
  • 44
  • 3