相关疑难解决方法(0)

如果关闭底层可读文件,关闭Java扫描程序是否安全?

如果我有一个带读者的方法,我想在读卡器上使用扫描仪操作,如下所示:

Scanner scanner = new Scanner(reader);
while(scanner.hasNext()) {
    //blah blah blah
}
Run Code Online (Sandbox Code Playgroud)

关闭是否安全scanner?文档说它"关闭了这个扫描仪",然后讨论关闭底层可读的.假设我不想关闭可读性,而是希望调用者reader在准备好时关闭.scanner在这里关闭是否安全?

java java.util.scanner

32
推荐指数
2
解决办法
4万
查看次数

scanner.close()有什么作用?

说我有以下示例代码:

Scanner scan1 = new Scanner(System.in);    // declaring new Scanner called scan1
int x = scan1.nextInt();    // scan for user input and set it to x
System.out.println(x);    // print the value of x
scan1.close();    // closes the scanner (I don't know exactly what this does)
Scanner scan2 = new Scanner(System.in); // declaring new Scanner called scan1
int y = scan2.nextInt();    // scan for user input and set it to y
System.out.println(y);    // print the value of y
Run Code Online (Sandbox Code Playgroud)

Scanner …

java java.util.scanner

11
推荐指数
1
解决办法
1万
查看次数

关闭BufferedReader和System.in

Reader rdr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(rdr);
String s;
s = br.readLine();
br.close();
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
System.out.print(s);
Run Code Online (Sandbox Code Playgroud)

我注意到,如果我关闭BufferedReader,我将无法再从键盘插入输入,因为System.in某种程度上是关闭的.无论如何我可以保留br.close()(我需要它来删除文件),然后从键盘添加更多输入?

java input system.in bufferedreader

6
推荐指数
1
解决办法
6178
查看次数

扫描仪方法打开和关闭两次

在单个类中,我有两种使用扫描类的方法.我为第一个方法创建了一个新的scanner对象实例,然后在第一个方法结束时将其关闭...然后在第二个方法中创建一个对象的新实例(具有不同的名称),最后在这个方法结束了.

除非我打开扫描仪一次并关闭它一旦它将无法工作并返回错误.扫描仪类的双重用途似乎不起作用.我究竟做错了什么?

这是我的两个返回错误的方法...

public void GameSetup(){
    //intro to the program and what the user should expect to do
    Scanner in = new Scanner(System.in);
    out.println("Let's play a little baseball game. Type down the scores for each team at the end of each inning to determine the winner!" +
            "\nFor each inning enter the score for the first team, hit space and enter the score for the second team.\nFirst, let's enter the team names:");

    //Console will ask for the team names.
    out.println("What …
Run Code Online (Sandbox Code Playgroud)

java java.util.scanner

2
推荐指数
1
解决办法
1万
查看次数