Scanner method opened and closed twice

前端 未结 3 1116
说谎
说谎 2020-12-10 22:39

In a single class I have two different methods who are using the scanner class. I created a new instance of scanner object for the first method, then closed it at the end of

相关标签:
3条回答
  • 2020-12-10 22:41

    You should declare Scanner as a class variable because you're already closing the scanner in the method gameSetup(). example:

     public class Game{
            Scanner in = new Scanner(System.in);
    
            public void GameSetup(){
                //insert code here DO NOT CLOSE SCANNER....
            }
    
    
            public void GetScores() {
                //your code here.........
                in.close();
            }
     }
    

    Or else what you can do is declare a scanner in each method and close in scope.

    0 讨论(0)
  • 2020-12-10 22:44

    As both Scanner instances use the same InputStream source, when the first instance is closed, then the second instance is unable to read from the InputStream resulting in a NoSuchElementException.

    public class GameClass {
        private final Scanner input;
    
        public GameClass() {
             input = new Scanner(System.in);
        }
    
        public void getScores() {
             team1Array[0] = input.nextInt();
             ...
        }
    }
    

    There's no need to close the Scanner instances unless you wish it to fail for subsequent reads. You could use a single instance of Scanner to avoid the overhead of 2 instances. By not closing the Scanner you also improve testability of the class.

    0 讨论(0)
  • 2020-12-10 22:46

    I have been learning Java for a short time and don't really know what I am doing, but this worked for me. I have no idea why it did but...

    import java.util.Scanner;
    public class Inputing{
    
        public Scanner input_scan = null;
    
        public static int ScanningInput() {
            int input_value = 0;
            Scanner input_scan = new Scanner(System.in);
    
            try {
                input_value = input_scan.nextInt();
            } catch (Exception e) {
                System.out.println("!error!");
            } finally {
                if (input_scan.equals(null)) {
                    input_scan.close();
                }
            }
            return input_value;
        }
    
        public static int getInputA(int scan_valueA){
            scan_valueA = (ScanningInput());
            return scan_valueA;
        }
    
            public static int getInputB(int scan_valueB){
            scan_valueB = (ScanningInput());
            return scan_valueB;
        }
    }
    
    // then call it with another class
    
    public class MainClass{
    
        public static void main(String[] args) {
    
            int variableA = 0; 
            variableA = Inputing.getInputA(variableA);
    
            int variableB = 0;
            variableA = Inputing.getInputC(variableB);
        }
    
    }
    

    I apologize for any errors, and would appreciate guidance. I am a complete rookie but after messing with this code for a while, this seemed to produce no warnings and return the results I was looking for.

    0 讨论(0)
提交回复
热议问题