Search code examples
javaruntime-error

Java Concatenating Ever Growing String java.lang.RuntimeException


I get an error from automated JUnit 4 unit testing software that my course instructor uses to automatically check assignments. We upload our draft code to a cloud program tester which automatically checks and grades the assignments.

Note: I am allowed to ask questions on StackOverflow per instructor.

This is week ~3 of first semester. I've never coded in Java before. We have only discussed up to functions (including if/else, while, loops, switch) so I can't use features from future modules.

We were not given a class tutorial on this specific assignment. The unit was on flow control.

My code (below) runs okay in the VSCODE IDE cloud the school provided.

import java.util.Scanner;

public class SplitString {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String output = "";

    do {
  
      String inputString = scanner.nextLine();

      if (!inputString.equals(" ")) {
        output += inputString;
      } else {
        break;
      }
    } while (true);
    System.out.println(output);
    scanner.close();
  }
}

I get this back

java.lang.RuntimeException 
java.lang.RuntimeException: Your application is expecting user input when no user input suppose to be given. Please take a look at the 'Caused by...' section of this stack trace for details of where the problem occurred in your code.
I get this feedback (which I don't understand)
java.lang.RuntimeException: Your application is expecting user input when no user input suppose to be given. Please take a look at the 'Caused by...' section of this stack trace for details of where the problem occurred in your code.
at tests.HomeworkTests.lambda$test_splitString$5(HomeworkTests.java:248)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at defaultPackage.SplitString.main(SplitString.java:13)
at tests.HomeworkTests.lambda$test_splitString$5(HomeworkTests.java:245)

The program test enters strings such as hello hello hello and supposed to stop at a blank line. Per the assignment "When this application receives a blank input, this application should stop receiving input and print the growing string to the console. The last string that this program should print is the result of the growing string."

At first I tried another method that just stopped at ENTER but then I got a different error (other code below.) That showed me the test was ending with a " " blank space not ENTER. Although the instructor said in discussion it presses enter. So I'm basically lost.

Here is the old code

import java.util.Scanner;

public class SplitString {

  public static void main(String[] args) {
    Scanner sx = new Scanner(System.in);
    String stringInput;
    String output = "";

    do {
      stringInput = sx.nextLine();
     
      output += stringInput;
      
    } while (!stringInput.isEmpty());

    System.out.println(output);
    sx.close();
  }
}

Gives this error

java.lang.StringIndexOutOfBoundsException: begin 0, end 12, length 3
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
at java.base/java.lang.String.substring(String.java:1874)
at homework.SplitString.main(SplitString.java:65)
at tests.HomeworkTests.lambda$test_splitString$5(HomeworkTests.java:288)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)

Any help would be appreciated to:

  • Explain where I'm going wrong or what I need to fix or something I could try
  • Explain what the errors mean that I'm getting (or where I can learn more about understanding the errors and how to read what to fix from them

Thank you for your time and support and patience <3.

I expected for the program to concatenate the user provided string until a blank is entered then print the resulting string to the console.


Solution

  • It seems blank line means empty line. Because it is not clear from requirements what the terminator should be, use trim() to remove all whitespaces, and if after trim the line is empty, then stop. Here is the corrected code:

    import java.util.Scanner;
    
    public class SplitString{
    
        public static void main( String[] args ){
            String output = "";
            try (  Scanner scanner = new Scanner( System.in ); ){
                do{
                    String inputString = scanner.nextLine();
                    if( inputString.trim().isEmpty() ){
                        break;
                    }
                    output += inputString;
                } while( true );
            }
            System.out.println( output );
        }
    }