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