error Exception in thread "main" java.util.InputMismatchException:

import java.util.Scanner;

public class LoginSystem {
    private static boolean admin = false;
    private static int userIndex = -1;
    private static String[][] userDB = {{"admin", "admin","0"}, {"201910370311052", "UMM_a2019","1","true"},{"201910370311051","UMM_a2019","1","false"}};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("=== Login System ===");
            System.out.print("Enter username: ");
            String username = scanner.nextLine();
            System.out.print("Enter password: ");
            String password = scanner.nextLine();

            boolean loggedIn = login(username, password);
            if (loggedIn) {
                System.out.println("Login successful!");

                if (admin) {
                    adminDashboard();
                } else {
                    studentDashboard();
                }
            } else {
                System.out.println("Login failed. Invalid username or password.");
            }
        }
    }

    public static boolean login(String username, String password) {
        for (int i = 0; i < userDB.length; i++) {
            if (userDB[i][0].equals(username) && userDB[i][1].equals(password)) {
                userIndex = i;
                if (i == 0) {
                    admin = true;
                }
                return true;
            }
        }
        return false;
    }

    public static void adminDashboard() {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("=== Admin Dashboard ===");
            System.out.println("1. Update student status");
            System.out.println("2. Update student password");
            System.out.println("3. Logout");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    updateStudentStatus();
                    break;
                case 2:
                    updateStudentPassword();
                    break;
                case 3:
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
                    break;
            }
        }
    }

    public static void studentDashboard() {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("=== Student Dashboard ===");
            System.out.println("1. Change password");
            System.out.println("2. Logout");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    updateStudentPassword();
                    break;
                case 2:
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
                    break;
            }
        }
    }

    public static void updateStudentStatus() {
        if (admin) {
            Scanner scanner = new Scanner(System.in);

            System.out.print("Enter student index: ");
            int index = scanner.nextInt();
            scanner.nextLine();

            System.out.print("Enter new status: ");
            String status = scanner.nextLine();

            // Update student status in userDB
            userDB[index][2] = status;

            System.out.println("Student status updated successfully.");
        } else {
            System.out.println("Access denied. Only admin can update student status.");
        }
    }

    public static void updateStudentPassword() {
        if(admin) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter student index: ");
            int index = scanner.nextInt();
            scanner.nextLine();

            System.out.print("Enter new password: ");
            String newPassword = scanner.nextLine();

            // Update student password in userDB
            userDB[userIndex][1] = newPassword;
        }else{
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter new password: ");
            String newPassword = scanner.nextLine();
            if(!isPassSymbols(newPassword)||!isPassDigit(newPassword)||!isPassLowerCase(newPassword)||!isPassUpperCase(newPassword));
            else System.out.println("password tidak sesuai ketentuan");
            userDB[userIndex][1] = newPassword;
        }
        System.out.println("Password updated successfully.");
    }
    public static boolean isPassSymbols(String password) {
        return password.matches(".*[!@#$%^&*()\\-=_+\\[\\]{};':\"\\\\|,.<>\\/?].*");
    }

    public static boolean isPassDigit(String password) {
        return password.matches(".*\\d.*");
    }

    public static boolean isPassUpperCase(String password) {
        return password.matches(".*[A-Z].*");
    }

    public static boolean isPassLowerCase(String password) {
        return password.matches(".*[a-z].*");
    }

    public static int getUserIndex() {
        return userIndex;
    }

    public static void setUserIndex(int userIndex) {
        LoginSystem.userIndex = userIndex;
    }

    public static String[][] getUserDB() {
        return userDB;
    }

    public static void setUserDB(String[][] userDB) {
        LoginSystem.userDB = userDB;
    }

    public static void setAdmin(boolean admin) {
        LoginSystem.admin = admin;
    }
}
avatar rafly24
@rafly24

2 Kontribusi 0 Poin

Diperbarui 1 tahun yang lalu

2 Jawaban:

<div>error biasanya karena salah type data, type data yang dimasukkan tidak sesuai dengan type datanya,&nbsp;<br>mungkin bisa dengan memasukkan skript ini di adminDashboard(),studentDashboard() atau di mana saja yang memakai metode scanner.nextInt() supaya menampilkan pesan kesalahan, dan meminta pengguna untuk memasukkan kembali.</div><pre>try { int choice = scanner.nextInt(); scanner.nextLine(); // process user input here } catch (InputMismatchException e) { System.out.println("Invalid input. Please enter a valid integer."); scanner.nextLine(); } <br></pre><div><br></div>

avatar yukaristel
@yukaristel

35 Kontribusi 32 Poin

Dipost 1 tahun yang lalu

<div>The code seems to have a syntax error in the updateStudentPassword method. The if statement after the else block has a semicolon at the end of the line, which means that the following else block and the System.out.println(&quot;Password updated successfully.&quot;) statement will always execute, regardless of the condition.&nbsp;<br><br>To fix the error, remove the semicolon at the end of the if statement as shown below:<br><br><br></div><pre>if(!isPassSymbols(newPassword)||!isPassDigit(newPassword)||!isPassLowerCase(newPassword)||!isPassUpperCase(newPassword)) { System.out.println("password tidak sesuai ketentuan"); } else { userDB[userIndex][1] = newPassword; System.out.println("Password updated successfully."); }</pre><div><br><br>This will make sure that the password is only updated if it meets the required conditions.</div>

avatar adamajalah27
@adamajalah27

119 Kontribusi 40 Poin

Dipost 1 tahun yang lalu

Login untuk ikut Jawaban