Java 如何在不经常关闭扫描的情况下使一个静态扫描程序成为全局变量,并在方法和主要方法中使用它?
我想创建一个静态扫描器,但我想在它周围放置try-catch块,这样它就可以自动关闭资源 泄漏和/或此异常:Java 如何在不经常关闭扫描的情况下使一个静态扫描程序成为全局变量,并在方法和主要方法中使用它?,java,jdbc,java.util.scanner,Java,Jdbc,Java.util.scanner,我想创建一个静态扫描器,但我想在它周围放置try-catch块,这样它就可以自动关闭资源 泄漏和/或此异常: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1585) at softwareEngineer.UserApp1.main(UserApp1.java:82) 基本上,我只想创建一
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at softwareEngineer.UserApp1.main(UserApp1.java:82)
基本上,我只想创建一个静态扫描程序声明,并在整个主程序中使用它,包括静态方法,此时,我的代码将需要为每个方法创建单独的扫描程序,您需要强制执行“scan.close()”。下面的代码将收到一个异常处理错误,原因是程序中有多个扫描仪处于打开状态,但没有关闭
我更新了代码,现在我得到空指针异常
import java.util.Scanner;
public class UserApp1 {
static User currentCustomer = null; //single object
static Scanner scan;
//-------------------------------------------------------
// Create a list, then repeatedly print the menu and do what the
// user asks until they quit
//-------------------------------------------------------
public static void main(String[] args) {
scan = new Scanner(System.in);)//scanner to avoid resource leak
printMenu(); //print menu system from another function
String choice = scan.nextLine(); //reads an input
final String EXIT_now = "0";
final String BACK = "back";
while (!(choice.equalsIgnoreCase(EXIT_now))){
switch(choice) {
case 1: break;
case 2:
currentCustomer = loginInput();<---- errors happens here
if(currentCustomer != null){
System.out.println("You have successfully login");
}
break;
default:
System.out.println("Sorry, invalid choice");
break;
} //ends switch
printMenu(); //print menu system from another function
choice = scan.nextLine(); //reads an input
}//ends while
System.out.println("\t\t GoodBye!\n Thank you for trying our program.");
System.exit(0);
}//ends main
//----------------------------
// Print the user's choices
//----------------------------
public static void printMenu() {
System.out.println("\t\t The User Login System ");
System.out.println("\t\t ======================");
System.out.println("The Menu Options:");
System.out.println("1: Register an Account");
System.out.println("2: Login to your Account");
System.out.println("3: Reset Password");
System.out.println("0: Quit/Exit ");
System.out.println("Please enter your selection > ");
} //ends printMenu
public static User loginInput(){
System.out.print( "\nFollow the Prompts to Log-In to your Account \n ");
System.out.print( "\nPlease enter your userid : \n ");
String userid = scan.nextLine();// <---- errors happens here
System.out.print( "\nPlease enter your password: \n ");
String pass = scan.nextLine();
currentCustomer = AccountList.loginUser(userid, pass);
if (currentCustomer != null)
{
return currentCustomer;
}
return null;
}//ends loginInput
}//ends class*
import java.util.Scanner;
公共类UserApp1{
静态用户currentCustomer=null;//单个对象
静态扫描;
//-------------------------------------------------------
//创建一个列表,然后重复打印菜单并执行
//用户请求直到他们退出
//-------------------------------------------------------
公共静态void main(字符串[]args){
scan=newscanner(System.in);)//避免资源泄漏的Scanner
printMenu();//从另一个函数打印菜单系统
String choice=scan.nextLine();//读取输入
最终字符串退出\u now=“0”;
最后一个字符串BACK=“BACK”;
while(!(choice.equalsIgnoreCase(立即退出))){
开关(选择){
案例1:断裂;
案例2:
currentCustomer=loginInput();您正在对资源使用try,它将在您完成try块时自动关闭。请尝试将其设置为如下所示的变量:
public class MyClass {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
}
}
避免在System.in input中使用多个扫描仪,因为它们会消耗数据流,然后您会遇到完全不同的问题。您使用的是“资源尝试”,它会在您完成“尝试”块时自动关闭。请尝试将其设置为如下变量:
public class MyClass {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
}
}
避免在System.in input中使用多个扫描仪,因为它们会消耗数据流,然后您会遇到完全不同的问题。您使用的是“资源尝试”,它会在您完成“尝试”块时自动关闭。请尝试将其设置为如下变量:
public class MyClass {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
}
}
避免在System.in input中使用多个扫描仪,因为它们会消耗数据流,然后您会遇到完全不同的问题。您使用的是“资源尝试”,它会在您完成“尝试”块时自动关闭。请尝试将其设置为如下变量:
public class MyClass {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
}
}
<>避免使用系统进行多个扫描器。在输入中,因为它们将消耗该流,然后您将有一个完全不同的问题。< P>避免使用静态全局扫描器,通过将要使用的扫描器实例传递给相关方法。
public static void main(String[] args) {
try(Scanner in = new Scanner(System.in)) {
String choice = in.nextLine().trim();
if(choice.equals("1")) {
doOp1(in);
} else if(choice.equals("2")) {
doOp2(in);
} else {
System.err.println("Invalid choice. Goodbye.");
}
}
}
// Method takes an open, functioning Scanner as an argument, therefore
// it doesn't need to close it, or worry about where it came from, it
// simply uses it, does what it needs to do, and returns, trusting
// the caller to properly close the Scanner, since it opened it.
private void doOp1(Scanner in) {
System.out.print("What is your name? ");
String name = in.nextLine().trim();
System.out.print("What is your favorite color? ");
String color = in.nextLine().trim();
}
private void doOpt2(Scanner in) {
...
}
您希望对资源进行分区,以确保它们的范围有限且易于关闭。将它们置于任何类型的全局状态都会非常困难。相反,请将资源的打开和关闭与使用它的代码分开。这种分区使代码更易于维护、可读和测试e
例如,通过向核心业务逻辑函数传递一个已经打开的扫描器
,您可以模拟真实用户的行为并创建一个测试来确保代码保持稳定,方法是构造一个从硬编码字符串读取的扫描器
,并将其传递到方法中,而无需运行整个类和tYPE在您一次又一次手动测试的行为中。< P>避免使用静态全局扫描器,通过将要处理的扫描器实例传递给相关方法。
public static void main(String[] args) {
try(Scanner in = new Scanner(System.in)) {
String choice = in.nextLine().trim();
if(choice.equals("1")) {
doOp1(in);
} else if(choice.equals("2")) {
doOp2(in);
} else {
System.err.println("Invalid choice. Goodbye.");
}
}
}
// Method takes an open, functioning Scanner as an argument, therefore
// it doesn't need to close it, or worry about where it came from, it
// simply uses it, does what it needs to do, and returns, trusting
// the caller to properly close the Scanner, since it opened it.
private void doOp1(Scanner in) {
System.out.print("What is your name? ");
String name = in.nextLine().trim();
System.out.print("What is your favorite color? ");
String color = in.nextLine().trim();
}
private void doOpt2(Scanner in) {
...
}
您希望对资源进行分区,以确保它们的范围有限且易于关闭。将它们置于任何类型的全局状态都会非常困难。相反,请将资源的打开和关闭与使用它的代码分开。这种分区使代码更易于维护、可读和测试e
例如,通过向核心业务逻辑函数传递一个已经打开的扫描器
,您可以模拟真实用户的行为并创建一个测试来确保代码保持稳定,方法是构造一个从硬编码字符串读取的扫描器
,并将其传递到方法中,而无需运行整个类和tYPE在您一次又一次手动测试的行为中。< P>避免使用静态全局扫描器,通过将要处理的扫描器实例传递给相关方法。
public static void main(String[] args) {
try(Scanner in = new Scanner(System.in)) {
String choice = in.nextLine().trim();
if(choice.equals("1")) {
doOp1(in);
} else if(choice.equals("2")) {
doOp2(in);
} else {
System.err.println("Invalid choice. Goodbye.");
}
}
}
// Method takes an open, functioning Scanner as an argument, therefore
// it doesn't need to close it, or worry about where it came from, it
// simply uses it, does what it needs to do, and returns, trusting
// the caller to properly close the Scanner, since it opened it.
private void doOp1(Scanner in) {
System.out.print("What is your name? ");
String name = in.nextLine().trim();
System.out.print("What is your favorite color? ");
String color = in.nextLine().trim();
}
private void doOpt2(Scanner in) {
...
}
您希望对资源进行分区,以确保它们的范围有限且易于关闭。将它们置于任何类型的全局状态都会非常困难。相反,请将资源的打开和关闭与使用它的代码分开。这种分区使代码更易于维护、可读和测试e
例如,通过向核心业务逻辑函数传递一个已经打开的扫描器
,您可以模拟真实用户的行为并创建一个测试来确保代码保持稳定,方法是构造一个从硬编码字符串读取的扫描器
,并将其传递到方法中,而无需运行整个类和tYPE在您一次又一次手动测试的行为中。< P>避免使用静态全局扫描器,通过将要处理的扫描器实例传递给相关方法。
public static void main(String[] args) {
try(Scanner in = new Scanner(System.in)) {
String choice = in.nextLine().trim();
if(choice.equals("1")) {
doOp1(in);
} else if(choice.equals("2")) {
doOp2(in);
} else {
System.err.println("Invalid choice. Goodbye.");
}
}
}
// Method takes an open, functioning Scanner as an argument, therefore
// it doesn't need to close it, or worry about where it came from, it
// simply uses it, does what it needs to do, and returns, trusting
// the caller to properly close the Scanner, since it opened it.
private void doOp1(Scanner in) {
System.out.print("What is your name? ");
String name = in.nextLine().trim();
System.out.print("What is your favorite color? ");
String color = in.nextLine().trim();
}
private void doOpt2(Scanner in) {
...
}
您希望对资源进行划分,以确保它们的范围有限且易于关闭。将它们置于任何类型的全局状态都非常困难。相反,请将它们分开