Outputting every second char in half string My algorithm works but it is too slow.
I have to ask for help with improving this algorithm because the website which checks this, kicks me for too slow execution.
What should I improve here ?

Details:


  Given a sequence of 2*k characters, please print every second
  character from the first half of the sequence. Start printing with the
  first character.
  
  Input In the first line of input your are given the positive integer t
  (1&lt;=t&lt;=100) - the number of test cases. In the each of the next t
  lines, you are given a sequence of 2*k (1&lt;=k&lt;=100) characters.
  
  Output For each of the test cases please please print every second
  character from the first half of a given sequence (the first character
  should appear).


Example


  Input:    

4
your
progress
is
noticeable

  
  Output:

y
po
i
ntc    



class Chars{

    public static void main (String[] args) {

        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        if (t &lt;= 100 &amp;&amp; t &gt;= 1) {
            for (int i = 0; i &lt; t; i++) {
                calculate();
            }
        }
    }

    private static void calculate() {

        Scanner sc = new Scanner(System.in);
        String rawString = sc.nextLine();
        StringBuilder afterAdd = new StringBuilder();
        StringBuilder finishChars = new StringBuilder();
        int numberOfChars = rawString.length();
        if (numberOfChars &lt;= 100 &amp;&amp; numberOfChars &gt;= 1) {
            if (numberOfChars % 2 == 0) {
                int numHalfChars = numberOfChars / 2;
                for (int i = 0; i &lt; numHalfChars; i++) {
                    afterAdd.append(rawString.charAt(i));
                }
                if (afterAdd.length() &lt;= 2) {
                    System.out.println(afterAdd.charAt(0));
                } else {
                    for (int i = 0; i &lt; afterAdd.length(); i = i + 2) {
                        finishChars.append(afterAdd.charAt(i));
                    }
                    System.out.println(finishChars);
                }
            }
        }
    }
}


after improve: 

class Algorithm{

    private static List&lt;String&gt; results = new ArrayList&lt;&gt;();

    public static void main (String[] args) {
        calculate();
        show();
    }

    private static void show() {
        for (String result : results) {
            System.out.println(result);
        }
    }

    private static void calculate() {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        if (t &lt;= 100 &amp;&amp; t &gt;= 1) {
            for (int i = 0; i &lt;= t; i++) {
                String rawString = sc.nextLine();
                StringBuilder finishChars = new StringBuilder();
                int numberOfChars = rawString.length();
                if (numberOfChars &lt;= 100 &amp;&amp; numberOfChars &gt;= 1 &amp;&amp; numberOfChars % 2 == 0) {
                    for (int j = 0; j &lt; rawString.length() / 2; j = j + 2) {
                        finishChars.append(rawString.charAt(j));
                    }
                    results.add(String.valueOf(finishChars));
                }
            }
        }
    }
}
Your code is slow simply because it does much more than it needs to. It copies the first half of the original string to a new StringBuilder, but then, you only use this StringBuilder to access its characters by their index, which you could also do with the original string, since the indexes of the characters in the first half of the original string and those in this new StringBuilder are identical.

So you might as well omit the variable afterAdd and use the original string in its place when you iterate over every second character:

for (int i = 0; i &lt; rawString.length() / 2; i = i + 2) {
    finishChars.append(rawString.charAt(i));
}


Also, there is no need to handle the case afterAdd.length() &lt;= 2 (which, after deleting afterAdd, would translate to rawString.length() &lt;= 4) separately. The result of the method will be unaffected if you remove this extra if and simply let the for loop take care of it. In fact, I don't see anything special in this case in the first place, so I wonder why you make a special case for it.

Besides, I think it is strange that you let the main method read the number of test cases from the command line, but delegate the reading of the individual test cases to the calculate method, not least because this requires you to create a new Scanner object for every single test case. Why don't you do all the reading from the command line in one method, and then pass each test case to the method calculate as a parameter and let it do only do what its name suggest, namely calculating the result?

Anyway, I would suggest separating user interface from program logic, because right now, the method calculate not only calculates the result, but also prints it to System.out, which goes a bit against the Single Responsibility Principle. I think the code would be clearer if calculate returns a String representing the result, so that the calling method can decide what to do with the result.