I'm trying to get a text at the customer.txt to validate if the customer has already registered

Here is my code:

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login{

    JFrame frame = new JFrame();
    JButton loginButton = new JButton("Login");
    JButton resetButton = new JButton("Reset");
    JButton registerAccount = new JButton("Sign Up");
    JTextField userIDField = new JTextField();
    JTextField userPasswordField = new JTextField();
    JLabel userIDLabel = new JLabel("Customer ID:");
    JLabel userPasswordLabel = new JLabel("Password:");
    JLabel messageLabel = new JLabel();
    JLabel title = new JLabel("Platinum Screen Cinema");
    JLabel title2 = new JLabel("Customer Login");

    Login(){
        title.setFont(new Font("Arial", Font.PLAIN, 30));
        title.setBounds(75, 50, 350, 30);

        title2.setFont(new Font("Arial", Font.PLAIN, 30));
        title2.setBounds(140, 10, 300, 30);

        userIDLabel.setBounds(50, 100, 75, 25);
        userPasswordLabel.setBounds(50, 150, 75, 25);

        messageLabel.setBounds(125,250,250,35);
        messageLabel.setFont(new Font(null,Font.ITALIC,25));

        userIDField.setBounds(125, 100, 300, 25);
        userPasswordField.setBounds(125, 150, 300, 25);

        loginButton.setBounds(125, 200, 100, 25);
        loginButton.setFocusable(false);
        loginButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                 try {
                        String location = "customer.txt";
                        String username = userIDField.getText();
                        String password = userPasswordField.getText();

                        FileReader fr = new FileReader(location);
                        BufferedReader br = new BufferedReader(fr);
                        String line, user, pass;
                        boolean isLoginSuccess = false;
                        while ((line = br.readLine()) != null) {
                            user = line.split(",")[1].toLowerCase();
                            pass = line.split(",")[2].toLowerCase();
                            if (user.equals(username) && pass.equals(password)) {
                                isLoginSuccess = true;
                                frame.dispose();
                                Booking mm = new Booking(username);
                                break;
                            } 
                        }
                        if (!isLoginSuccess) {
                            JOptionPane.showMessageDialog(null, "WRONG PASSWORD", "WARNING!!", JOptionPane.WARNING_MESSAGE);
                        }
                        fr.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
               }
        });

        resetButton.setBounds(325, 200, 100, 25);
        resetButton.setFocusable(false);
        resetButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == resetButton) {
                    userIDField.setText("");
                    userPasswordField.setText("");
                }
    }
});

        registerAccount.setBounds(225, 200, 100, 25);
        registerAccount.setFocusable(false);
        registerAccount.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource()== registerAccount) {
                    frame.dispose();
                    new Register();
                }
    }
});

        frame.add(title);
        frame.add(title2);
        frame.add(userIDLabel);
        frame.add(userPasswordLabel);
        frame.add(messageLabel);
        frame.add(registerAccount);
        frame.add(userIDField);
        frame.add(userPasswordField);
        frame.add(loginButton);
        frame.add(resetButton);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(520, 420);
        frame.setLayout(null);
        frame.setVisible(true);
    }


}

The Error:

java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
    at Login$1.actionPerformed(Login.java:62)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6616)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398)
    at java.desktop/java.awt.Component.processEvent(Component.java:6381)
    at java.desktop/java.awt.Container.processEvent(Container.java:2266)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4991)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Insert the Text File

CharlieBrown,kdc*iJWw
Snoppy,3KnEgof9%
Linus,$R$ARo1g
PeppermintPatty,PD9wtik09+
Lucy,J!nyMdfp@7x

Recommended Answers

All 5 Replies

Java arrays are zero-based, so the two array elements created by line.split are [0] and [1], not [1] and [2]

ps: To anyone else thinking of asking a question like this, please note how Wafflez made it easy to get a good response:
Wafflez posted all the relevant code, properly formatted
Wafflez posted the relevant data file
Wafflez posted the complete error message
Wafflez used variable names that made the code easy to read and understand

read it and learn.
J

commented: Thank you sorry for my bad english +0

I still get errors but I tweak it a little bit the code.
Here the new code :

loginButton.setBounds(125, 200, 100, 25);
        loginButton.setFocusable(false);
        loginButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt) {
                 try {
                        String lokasi = "customer.txt";
                        String username = userIDField.getText();
                        String password = userPasswordField.getText();

                        FileReader fr = new FileReader(lokasi);
                        BufferedReader br = new BufferedReader(fr);
                        String line, user, pass;
                        boolean isLoginSuccess = false;
                        while ((line = br.readLine()) != null) {
                            user = line.split(" ")[0].toLowerCase();
                            pass = line.split(" ")[1].toLowerCase();
                            if (user.equals(username) && pass.equals(password)) {
                                isLoginSuccess = true;
                                frame.dispose();
                                Booking mm = new Booking(username);
                                break;
                            } 
                        }
                        if (!isLoginSuccess) {
                            JOptionPane.showMessageDialog(null, "USERNAME/PASSWORD WRONG", "WARNING!!", JOptionPane.WARNING_MESSAGE);
                        }
                        fr.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
               }
        });

The Error i get:

java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at Login$1.actionPerformed(Login.java:62)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6616)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398)
    at java.desktop/java.awt.Component.processEvent(Component.java:6381)
    at java.desktop/java.awt.Container.processEvent(Container.java:2266)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4991)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Insert the text:

test    123
test2   123
test2   123

Obviously something going wrong with the split because it's returning an array of length 1.
You are splitting on a single space but the data seems to contain multiple spaces between the values. That should give you extra empty strings in the array, not a 1 element array. Maybe there a tab character between the fields in the data file? (In which case split on tab, not space).
Have a look at what's happening by adding a print of the split result
System.out.println(Arrays.toString(line.split(" ")));

JC

ps: your English is good enough - no problem. :)

Thank You Very Much I'm using this code to prevent the error because of the space hahaha

String[] splitString = line.split("\\s+");
user = splitString.length >1? splitString[0] : " ";
pass = splitString.length >1? splitString[1] : " ";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.