#Java scanner not picking up latter calls of a functions rigged input.

17 messages · Page 1 of 1 (latest)

primal veldt
#

Hello, I am having trouble using the Java scanner inputstream as a rigged input for my java game that I am trying to test using cucumber. My issue can be summarized as:

Below are two testing functions I am using to test my game. My issue is that, my input will work for the first run of playMelee which is repeatedly called by playGame taking 3 inputs at a time, showing that I want it to take 3 rounds of inputs of 3 '1's here.

My issue is that it will work for the first round of inputs but no subsequent ones showing a no line found error for my Scanner instance which is created inside my playMelee function.

I have tried creating a general scanner in the class and creating a scanner which is input as a parameter to no avail.

Essentially: I am giving the input 9 different inputs of '1' and it is only receiving the first 3 as the function playMelee takes 3 at a time as it is called 3 times by playGame showing it does not work for subsequent inputs. Any help is appreciated

  private void provideTestInput(String input) {
    InputStream inputStream = new ByteArrayInputStream(input.getBytes());
    System.setIn(inputStream);
    System.setOut(printStream);
  }

  private void runMelee() {
    String input2 = "1\n1\n1\n1\n1\n11\n1\n1\n";
    provideTestInput(input2);
    game.playGame(); // this function repeatedly calls playMelee which takes 3 inputs at a time
  }


// in Game class

void playGame() {
   for(int=0;i<3;i++){
     playMelee()
  }
}

void playMelee() {
   input = new Scanner(System.in);
   for(int=0;i<3;i++){
       String valueInput = input.nextLine();
  }
  // determine winner of game etc.
}```
dusky impBOT
#

This post has been reserved for your question.

Hey @primal veldt! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

patent mantle
#

I dont know why do you even use:

  private void provideTestInput(String input) {
    InputStream inputStream = new ByteArrayInputStream(input.getBytes());
    System.setIn(inputStream);
    System.setOut(printStream);
  }

here

primal veldt
patent mantle
#

you dont have to do that, its default

primal veldt
#

I use scanner.nextLine in my game loop

#

which is what I want to rig

patent mantle
#

yes so just use Scanner scanner = new Scanner(System.in)

#

that's all you need man

primal veldt
#

I am trying to give it input for my test of running a full game

patent mantle
#

your goal is to get output from console, right?

primal veldt
#

the input I have it rigged with only works for the first run of functions inside of the gameloop

patent mantle
#

so what is printStream in your code?