0

Hello StackOverflow peeps!

Working to learn/improve my coding skills with hackerrank.com's 30 days of code. I'm on day 6 and having some issues figuring out why I'm getting this error message:

"~ no response on stdout ~"

I've done some searching on google and within stackoverflow and found others with the same error but it was because they weren't using standard I/O. I feel like I am, what am I missing?

Here's my code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

    Scanner sc = new Scanner(System.in);
    int cases = sc.nextInt();

    while(cases > 0){
        getWord();
        cases--;
    }


}

public static void getWord(){
    //Save input to string
    Scanner sc = new Scanner(System.in);
    String userInput = sc.nextLine();

    //Convert string to character array
    char[] inputCharArray = userInput.toCharArray();

    //Setup output strings
    String evenOutputString = "";
    String oddOutputString = "";

    //Iterate through array
    for (int i = 0; i <= userInput.length(); i++) {
        //Check if index is even
        if (i % 2 == 0) {
            //Add to even output string
            evenOutputString = evenOutputString + inputCharArray[i];
        } else {
            //Add to odd output string
            oddOutputString = oddOutputString + inputCharArray[i];
        }
    }

    //Output final output in one line, seperated by a single space
    System.out.println(evenOutputString + " " + oddOutputString);

    }

}

The challenge can be found here: https://www.hackerrank.com/challenges/30-review-loop/problem

The first input is a number that determines how many test cases will occur, the following inputs are single words that I'm supposed to put into an array and then sort out the even and odd index slots and print them on one line, separated by a space.

Example Input:

2
Hacker
Rank

Example Output:

Hce akr
Rn ak

I've tried moving the while(cases > 0) code block to it's own method just in case the args from the main method was part of the problem, and got the same results. I've also tried the .toString on the inputCharArray[i] for the output string concatenation. I'm also trying to stay away from StringBuilder so that I can stay within the intended scope of the challenge.

  • do you need to prompt the user to enter the original int? – Scary Wombat Jul 12 '17 at 04:50
  • 2
    Your code produces an `ArrayIndexOutOfBoundsException`. Are you sure your problem is what you think it is? – Tavo Jul 12 '17 at 04:50
  • I've never seen this error message in an actual Java console program. Try writing a simple 1-3 line program which reads a user's input from the command line. If that fails in the HackerRank environment then you'll know what the problem is. – Tim Biegeleisen Jul 12 '17 at 04:50
  • @ScaryWombat HackerRank usually defines input to be read from `System.in`. In this case, it likely states that first line is a number of test cases `N`, and the next `N` lines will contain a word to be processed. Each test case would then likely print one line of result, i.e. expected output is `N` particular lines. – Andreas Jul 12 '17 at 04:52
  • I believe @Tavo is correct, i.e. program fails with exception before first output and exception is printed on STDERR, not STDOUT, hence the result of `~ no response on stdout ~`. Even if you didn't have exception, the first word you read would be a blank string, and you'd end up printing a blank line, so it test only has one word, you might also get `~ no response on stdout ~`, hence the [duplicate link](https://stackoverflow.com/q/13102045/5221149). – Andreas Jul 12 '17 at 04:57
  • [a full solution to the challenge](https://stackoverflow.com/questions/44135740/how-to-print-even-and-odd-position-characters-of-an-array-of-strings-in-java/44136470#44136470) – XtremeBaumer Jul 12 '17 at 06:18
  • Hmm, that helps @Andreas. I guess I need to put my code into an IDE to try and get better error details moving forward. The hackerrank error reporting isn't super detailed. Sorry if it was a duplicate post, in my searching I didn't find that one because I was primarily looking for the error code provided. 'ArrayIndexOutOfBoundsException' I'll start with that. – Chris Neuman Jul 12 '17 at 15:13

0 Answers0