Java 扫描仪工作不正常

Java 扫描仪工作不正常,java,exception,java.util.scanner,Java,Exception,Java.util.scanner,各位程序员好。我还有一个困扰我的问题。我试图从用户处接收输入,但总是收到“java.util.NoSuchElementException:找不到行”。我尝试了我所寻找的所有方法,但没有成功。这是我的密码: import java.util.Scanner; import java.io.*; public class UserLog { static String username; static String password; static String pass

各位程序员好。我还有一个困扰我的问题。我试图从用户处接收输入,但总是收到“java.util.NoSuchElementException:找不到行”。我尝试了我所寻找的所有方法,但没有成功。这是我的密码:

import java.util.Scanner;
import java.io.*;

public class UserLog {
    static String username;
    static String password;
    static String passcompare;
    static File name;
    static String Userfile = "username-";
    static String Passfile = "password-";

    public static void main(String[] args) {
        menu();
    }

    public static void menu(){
        boolean call = false;
        try (Scanner in = new Scanner(System.in)) {
            do {
                System.out.println("Select an option: ");
                System.out.println("1: New account \n"
                                 + "2: Existing account");
                System.out.print("-");
                int choice = in.nextInt();
                in.nextLine();

                 switch(choice) {
                     case 1:
                         call = true;
                         System.out.println("\nNew account called\n");
                         userCreate();
                         break;
                     case 2:
                         call = true;
                         System.out.println("\nExisting account called\n");
                         login();
                         break;
                     default:
                         System.out.println("\nNot a valid option\n");
                 }
            } while(!call);
            in.close();
        }
        catch(Exception ex) {
            System.out.println("Exception Text: " + ex);
        }
    }

    static void login(){       
        try (Scanner in = new Scanner(System.in)) {
            System.out.println("LOGIN SCREEN\n");
            System.out.print("Username: ");

            username = in.nextLine();
            name = new File("user-" + username + ".txt");
            if(name.exists()) {
                System.out.println("Username exists");

                System.out.print("Password: ");

                password = in.nextLine();
                //scans userfile for password
                if(password.length() != 0 && password.length() >= 8 /* and password-username match */) {
                    System.out.println("Login successful");
                    in.close();
                }
            }

            else {
                System.out.println("Username doesn't exist in system");
                System.out.println("Would you like to create this user? (y/n)");
                System.out.print("-");

                char choice = (char)System.in.read();

                switch(choice) {
                    case 'Y':
                    case 'y':
                        System.out.println("Creating user " + username);
                        name = new File("user-" + username + ".txt");
                        name.createNewFile();
                        System.out.println("User created");
                        passCreate(name);
                        in.close();
                        break;
                    case 'N':
                    case 'n':
                        System.out.println("Denied creation of user");
                        in.close();
                        break;
                    default:
                        System.out.println();
                }
            }
            in.close();
        } catch (IOException ex) {
            System.out.println("Exception Text: " + ex);
        }
    }

    private static File nameCreate() {
        try (Scanner user = new Scanner(System.in)) {
            System.out.print("Enter Username: ");

            username = user.nextLine();
            name = new File("user-" + username + ".txt");

            if(!name.exists()) {
                name.createNewFile();
                try (FileWriter fw = new FileWriter(name)) {
                        fw.write(Userfile + username + "\n");
                        fw.write(Passfile);
                        fw.flush();
                        fw.close();
                }
                catch(Exception ex) {
                    System.out.println("Exception Text: " + ex);
                }

                //puts lines of text in the file-
                //username-"username"
                //password-
                //
                System.out.println("User Created\n");
            }
            else if(name.exists()) {
                System.out.println("User already exists\n");
                nameCreate();
            }
            user.close();
        }
        catch(Exception ex) {
            System.out.println("Exception Text: " + ex);
        }
        return name;
    }

    private static void passCreate(File user) {
        username = user.toString();
        System.out.println(username + "\n");

        boolean code = false;

        try (Scanner pass = new Scanner(System.in)) {
            do{
                //opens file and reads until line "password-" and appends it with created password once confirmed
            System.out.println("Create a password");
            System.out.print("Password: ");
            password = pass.nextLine(); 

                if(password.length() >= 8) {
                    System.out.print("Confirm Password: ");
                    passcompare = pass.nextLine();

                    if(password.equals(passcompare)) {
                        code = true;
                        System.out.println("Passwords match\n");
                        //stores password
                    }
                    else {
                        System.out.println("Passwords don't match\n");
                    }   
               }
               else {
                   System.out.println("Password needs to be longer than 8 characters\n");
               }

            }while(!code);
            pass.close();
        }
        catch(Exception ex) {
            System.out.println("Exception Text: " + ex);
        }     
    }

    private static void userCreate() {
        nameCreate();
        passCreate(name);
    }
}

我知道这是丑陋和不完整的,但这个问题使我无法继续前进。如果通过“现有用户”选项并以这种方式创建用户,则可以创建密码,但如果尝试通过“创建新用户”,则会出现无行异常。我的问题是:如何为扫描仪创建更多的行以创建密码,或者我还有什么其他选项来完成此任务。我试过hasNextLine(),这让我意识到没有更多的行了。谢谢

关闭
扫描器
实例会导致底层
输入流
关闭,并导致后续读取时引发
NoTouchElementException
。无需关闭
扫描仪
,除非您希望后续读取失败

  • 创建一个用于所有方法的
    扫描仪
    实例
  • 不要关闭
    扫描仪
  • Java是一种
    OO
    语言。使用非
    静态
    方法
结果是:

public class UserLog {
   private String username;
   // more variables...

   private final Scanner in;

   public UserLog() {
      in = new Scanner(System.in);
   }

   public static void main(String[] args) {
      UserLog userLog = new UserLog();
      userLog.showMenu();
   }

   public void menu() {
      boolean call = false;

      do {
         try {
            System.out.println("Select an option: ");
            System.out.println("1: New account \n" + "2: Existing account");
            System.out.print("-");
            int choice = in.nextInt();
            in.nextLine();

            switch (choice) {
            case 1:
               call = true;
               System.out.println("\nNew account called\n");
               userCreate();
               break;
            case 2:
               call = true;
               System.out.println("\nExisting account called\n");
               login();
               break;
            default:
               System.out.println("\nNot a valid option\n");
            }

         } catch (InputMismatchException e) {
            System.out.println("Invalid option " + in.nextLine());
         }
      } while (!call);
   }
   ...
} 

是否可以只向我们显示与
NoTouchElementException
相关的部分代码,并回答您的问题:是的,但它仅在程序开始时通过选项1时发生。它在passCreate函数中。在选项2中(用户已经存在),在输入用户名之前,您似乎不会试图关闭
扫描仪。但在选项1中,您获得用户名,关闭
扫描仪
,然后尝试使用
系统制作另一个
扫描仪
。在
中输入密码。我明白了。谢谢你的链接。它澄清了这一点,从来没有想到这一点。因此,如果我们使用扫描仪从System.in读取,我们不应该关闭扫描仪,因为底层流也会被关闭?在这种情况下,一个愚蠢的后续问题是:有没有办法重新初始化(重新打开)
系统。在
中使用标准输入流?不,只是不要关闭
扫描仪
,所以我基本上删除了所有的.close()这是一个很好的例子。但是需要抛出的异常太多了。这是由于静态方法造成的,还是仅仅因为它是如何产生的?对不起,现在很晚了,你怎么才能让这一切都变成非静态的呢?