原创

我输入了正确的用户名和密码,但它仍然不起作用[关闭]

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

Whenever I input the correct username and password it says incorrect username or password.

package taskperf6;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class TaskPerf6 {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        
        System.out.println("          Welcome!           ");
        System.out.println("");
        System.out.println("-Please choose between 1 & 2-");
        System.out.println("-1. Register -");
        System.out.println("-2. Login -");
        
        int choice = input.nextInt();
        input.nextLine(); 
        
        System.out.println("Your choice:" + "[" + choice + "]");
        System.out.println("");
        
         BufferedWriter writer = null;
        
         if (choice == 1) { 
            try {
                
                System.out.println("       R E G I S T E R     ");
                System.out.print("Hi! Please input your desired username: ");
                String username1 = input.nextLine();
                
                System.out.println("");
                System.out.print("Hi! Please input your desired passowrd: ");
                String password1 = input.nextLine();
                
                writer = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Documents\\NetBeansProjects\\Temp File\\TaskPerf6_Temp\\records.txt", true));
                writer.write("\nUsername: " + username1 + " | " + " Password: " + password1);
                System.out.println("");
                System.out.println("You have successfully registered!");
                System.out.println("        Welcome! " + username1);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (writer != null) {
                        writer.close();
                    }
                } catch (IOException e) {
                } 
            }  
    }  else if (choice == 2) {
             System.out.println(" L O G I N ");
             System.out.print("Enter your username: ");
             String usernameLog = input.nextLine();
             
             System.out.print("Enter your password: ");
             String passwordLog = input.nextLine();
             
             try (Scanner scan = new Scanner(new File("C:\\Users\\Administrator\\Documents\\NetBeansProjects\\Temp File\\TaskPerf6_Temp\\records.txt"))) {
                 while (scan.hasNextLine()) {
                     String line = scan.nextLine();
                     if (line.contains(usernameLog + " | ")) {
                         String[] parts = line.split(" \\| ");
                         if (parts[1].trim().equals(passwordLog)) {
                             System.out.println("Successfully logged in!");
                             return;
                         }
                     }
                 }
                 System.out.println("Incorrect username or password.");
             } catch (IOException e) {
                 e.printStackTrace();
             }
        }
    }
}

Contents of the File (Login)

The output of the code (Login)

I have no problems regarding in the registration it's just on the login part.

正文到此结束
热门推荐
本文目录