vendredi 8 janvier 2021

(unit testing) How to keep Scanner running (Java)

I have an assignment that should read user input (test files) and return some data (output). Everything is running fine and I pass 2 of my tests, but due to some sily mistake on my end the stream gets closed (after passing the tests).

Main handles the user input and has Scanner declared and running, it calls the methods from the class I created (I cannot change Main due to assignment restriction).

This is the code I've written on my class:

public class MyInstance extends Intersection {

private boolean methodHasRun = false;
//  keeps count of getIntersectionState() loop
    private static int loop_count;

public String getIntersectionState(){

        String left = "LVg0";
        String right = "RVg0";
        String bottom = "BVr0";
        String top = "TVr0";
        String temp = " ";
        String temp1 = " ";
        String temp2 = " ";
        String temp3 = " ";
        String nr [] = {"1","2","3","4","5","6","7","8","9","10"};

//      if advanceTime() was executed
        if (this.methodHasRun) {
            
            //get last char, write it to temp
            temp = bottom.substring(bottom.length()-1).trim();  
            temp1 = top.substring(top.length()-1).trim();
            while(loop_count < nr.length-1) { 
                temp = "BVr" + nr[loop_count];
                temp1 = "TVr" + nr[loop_count];
                ++loop_count;
//              break after each increment
                break;
            }
            if(loop_count == 9) {
                temp = "BVr" + nr[8];
                temp1 = "TVr" + nr[8]; 
                ++loop_count;
            }else if(loop_count > 9) {
                while(loop_count < 20) {                
                temp = "BVg" + nr[8];
                temp1 = "TVg" + nr[8];
                temp2 = "LVr" + nr[loop_count-10];
                temp3 = "RVr" + nr[loop_count-10];
                ++loop_count; 
//              can't save left and right out of else if
//              because they will be assigned to: " "
                left = temp2;
                right = temp3;
                break;
                }
            }
                
            if(loop_count == 21) {
                temp = "BVg" + nr[9];
                temp1 = "TVg" + nr[9];
                temp2 = "LVr" + nr[9];
                temp3 = "RVr" + nr[9];
                
                left = temp2;
                right = temp3;
            }
            while(loop_count == 20) {
                temp2 = "LVr" + nr[9];
                temp3 = "RVr" + nr[9];
                ++loop_count;
                break;
            }
            if (loop_count > 21) {
                System.out.println("go on");
            }
            bottom = temp;
            top = temp1;

//          System.out.println("State changed!"+
//                  "\nLoop count is: "+loop_count);
            
//          revert to false until state changes again
            this.methodHasRun = false;
        
            } else {
            System.out.println("State hasn't changed");
            }
        return left +" "+ right +" "+ bottom +" "+ top;
    }
}

It produces this when run: enter image description here

The test input file is:

state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state
vehicle left
vehicle right
vehicle bottom
vehicle top
step
state

That should output:

LVg0 RVg0 BVr0 TVr0
LVg0 RVg0 BVr1 TVr1
LVg0 RVg0 BVr2 TVr2
LVg0 RVg0 BVr3 TVr3
LVg0 RVg0 BVr4 TVr4
LVg0 RVg0 BVr5 TVr5
LVg0 RVg0 BVr6 TVr6
LVg0 RVg0 BVr7 TVr7
LVg0 RVg0 BVr8 TVr8
LVg0 RVg0 BVr9 TVr9
LVr1 RVr1 BVg9 TVg9
LVr2 RVr2 BVg9 TVg9
LVr3 RVr3 BVg9 TVg9
LVr4 RVr4 BVg9 TVg9
LVr5 RVr5 BVg9 TVg9
LVr6 RVr6 BVg9 TVg9
LVr7 RVr7 BVg9 TVg9
LVr8 RVr8 BVg9 TVg9
LVr9 RVr9 BVg9 TVg9
LVr10 RVr10 BVg9 TVg9
LVg10 RVg10 BVr10 TVr10

I hope someone can catch my mistake or suggest something.

Aucun commentaire:

Enregistrer un commentaire