Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

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

Edit: It was a mistake on my part, changed the testing input by mistake. It runs like a charm now :) Thanks for your help guys! You can close the question if you want.

Original: 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!"+
//                  "
Loop 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Every single line of stuff you pasted is completely irrelevant to your question. The relevant parts are where you use and close that Scanner, and where you throw the 'cannot parse command' exception.

A few notes:

  1. If you are calling scanner.close() someplace or wrapping it in a try-with-resources, don't do that. If you can't fix that code, find the person who can, and tell them to fix their bug.
  2. If you must work around it, you can override System.in with a wrapper PrintStream that ignores close() calls. This is somewhat complicated and is working around a bug - i.e. vastly inferior vs. just fixing your bug instead. Short of either this or removing scanner.close() / try-with-resources, there is no magic --do-not-close-scanners option.
  3. Your exception handling is broken if that stack trace is caused by the scanner being closed. You should at the very least do something like throw new IllegalArgumentException("Cannot parse command", e); - that , e part sets up the cause. This is good, because you currently aren't doing that, and it makes it impossible to know what actually went wrong (I just have to go by your title that it's apparently a closed System.in).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...