Runtime Error always comes up whenever Scanner is defined

Viewed 20

I am trying to practice Google Kickstart problems but there seems to be an issue related to how data input via Scanner works behind the scenes so if I define Scanner class in my solution, there is always RE (Runtime Error) even when testing.

For Coding Practice with Kick Start Session #1 - Kick Start 2022, Centauri Prime Problem, this problem still happens when I download the Starter Code that Google provided which is here:

import java.util.*;

public class Solution {

  private static String getRuler(String kingdom) {

    String ruler = "";
    return ruler;
  }

  public static void main(String[] args) {
    try (Scanner in = new Scanner(System.in)) {
      int T = in.nextInt();

      for (int t = 1; t <= T; ++t) {
        String kingdom = in.next();
        System.out.println("Case #" + t + ": " + kingdom + " is ruled by " + getRuler(kingdom) + ".");
      }
    }
  }
}

And modify it by changing line 7 from String ruler = ""; to String ruler = "Bob". This is of course wrong solution but with such small modification to Google Code, it should certainly work without issues and not give Runtime Error. So the code I submit looks like this:

import java.util.*;

public class Solution {

  private static String getRuler(String kingdom) {

    String ruler = "Bob";
    return ruler;
  }

  public static void main(String[] args) {
    try (Scanner in = new Scanner(System.in)) {
      int T = in.nextInt();

      for (int t = 1; t <= T; ++t) {
        String kingdom = in.next();
        System.out.println("Case #" + t + ": " + kingdom + " is ruled by " + getRuler(kingdom) + ".");
      }
    }
  }
}

I would appreciate any help solving this issue as I have spend couple hours and can't figure it out.

Same thing happens for other problems. As long as there is Scanner defined in the solution as Scanner in = new Scanner(System.in), RE comes up all the time.

0 Answers
Related