所以我正在制作关于ATM系统的gui 我想要做的是当客户输入他/她的帐号和pin时,程序将在DATABASE.txt中搜索它,当它与DATABASE.txt中的信息匹配时。程序将自动显示Name,AccountNumber ,年龄,性别和平衡。但在我的情况下我得到这个错误,我不知道如何解决它。 plzz帮助我。如果你帮助我,我将不胜感激:)谢谢你,欢呼:)。 PS。我希望你们理解我的源代码T ^ T。
1st CLASS
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Xavier1 extends JFrame implements ActionListener,Runnable
{
JButton a[] = new JButton [10];
JLabel pin,acc;
JTextField acc1;
JButton del,ok,crt;
int c=0,d=0,e=0;
JPasswordField pin1;
JFrame x;
String us,pw;
public void run()
{
}
public Xavier1()
{
x = new JFrame("ATM");
x.setLayout(null);
x.setBounds(400,100,550,500);
x.setVisible(true);
JPanel x3 = new JPanel();
x.add(x3);
x3.setVisible(true);
x3.setBounds(150,420,250,100);
crt = new JButton("Create Account");
crt.addActionListener(this);
x3.add(crt);
JPanel x2 = new JPanel();
x.add(x2);
x2.setBounds(150,10,250,100);
x2.setVisible(true);
x2.setLayout(new GridLayout(4,2));
pin = new JLabel("PIN");
acc = new JLabel("ACCOUNT NUMBER");
pin1 = new JPasswordField(20);
acc1 = new JTextField(20);
x2.add(acc);
x2.add(acc1);
x2.add(pin);
x2.add(pin1);
JPanel x1 = new JPanel();
x.add(x1);
x1.setBounds(150,200,250,200);
x1.setVisible(true);
x1.setLayout(new GridLayout(4,3));
for(int b=0;b<10;b++)
{
a[b] = new JButton();
a[b].setText(Integer.toString(b));
x1.add(a[b]);
a[b].addActionListener(this);
}
ok = new JButton("OK");
ok.addActionListener(this);
del = new JButton("DEL");
del.addActionListener(this);
x1.add(ok);
x1.add(del);
}
public static void main (String args[])
{
Xavier1 xs = new Xavier1();
Thread xd = new Thread(xs);
xd.setDaemon(true);
}
public void actionPerformed(ActionEvent z)
{
Object zz = z.getSource();
if(e == 0)
{
for(int c=0;c<a.length;c++)
{
if(zz == a[c])
{
acc1.setText(acc1.getText()+a[c].getText());
}
}
}
if(zz == ok)
{
e++;
}
if(e==2)
{
GateKeeper();
}
if(e == 1)
{
for(int d=0;d<a.length;d++)
{
if(zz == a[d])
{
pin1.setText(pin1.getText()+a[d].getText());
}
}
}
if(zz == crt)
{
Reg rs = new Reg();
Thread sr = new Thread(rs);
sr.start();
x.setVisible(false);
}
if(zz == del)
{
if(e == 0)
{
acc1.setText(null);
}
if(e == 1)
{
pin1.setText(null);
}
}
}
public void GateKeeper()
{
try
{
Scanner sc = new Scanner(new File("DATABASE.txt"));
int loop = sc.nextInt();
sc.nextLine();
for(int gk=0;gk<loop;gk++)
{
String ps[] = sc.nextLine().split(" ");
us = ps[0];
pw = ps[1];
if(acc1.getText().equals(us));
{
if(pin1.getText().equals(pw))
{
Bank b = new Bank();
Thread xb = new Thread(b);
xb.start();
x.setVisible(false);
}
}
}
}
catch(FileNotFoundException s)
{
}
}
}
2ND CLASS
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Reg extends Bank
{
JButton res,back,next;
JFrame r;
public Reg()
{
r = new JFrame("REGISTRATION");
r.setVisible(true);
x.setVisible(false);
r.setBounds(400,100,550,500);
r.setLayout(null);
X.setVisible(false);
n = new JLabel("Name");
age = new JLabel("AGE");
sex = new JLabel("SEX");
dep = new JLabel("DEPOSIT");
accn = new JLabel("ACCOUNT NUMBER");
pins = new JLabel("PIN");
n1 = new JTextField(20);
accn1 = new JTextField(20);
bal1 = new JTextField(20);
sex1 = new JTextField(20);
age1 = new JTextField(20);
dep1 = new JTextField(20);
pins1 = new JPasswordField(20);
JPanel r1 = new JPanel();
r.add(r1);
r1.setVisible(true);
r1.setBounds(50,10,400,400);
r1.setLayout(new GridLayout(7,2));
r1.add(accn);
r1.add(accn1);
r1.add(pins);
r1.add(pins1);
r1.add(n);
r1.add(n1);
r1.add(age);
r1.add(age1);
r1.add(sex);
r1.add(sex1);
r1.add(dep);
r1.add(dep1);
JPanel r2 = new JPanel();
r.add(r2);
r2.setVisible(true);
r2.setBounds(130,400,300,50);
r2.setLayout(new GridLayout(0,3));
back = new JButton("BACK");
next = new JButton("NEXT");
res = new JButton("REGISTER!");
res.addActionListener(this);
back.addActionListener(this);
next.addActionListener(this);
r2.add(back);
r2.add(res);
r2.add(next);
}
public void DATABASE()
{
try
{
FileWriter fw = new FileWriter("DATABASE.txt",true);
PrintWriter pw = new PrintWriter(fw);
pw.print(accn1.getText()+" ");
pw.print(pins1.getText()+" ");
pw.print(n1.getText()+" ");
pw.print(age1.getText()+" ");
pw.print(sex1.getText()+" ");
pw.print(dep1.getText()+" ");
pw.println();
pw.close();
}
catch(IOException s)
{
}
}
public static void main(String args[])
{
}
public void actionPerformed(ActionEvent rg)
{
Object rz = rg.getSource();
if(rz == res)
{
DATABASE();
}
if(rz == next)
{
Bank b = new Bank();
Thread xb = new Thread(b);
xb.start();
r.setVisible(false);
}
if(rz == back)
{
Xavier1 xs = new Xavier1();
Thread xd = new Thread(xs);
xd.start();
r.setVisible(false);
}
}
}
第3课
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Bank extends Xavier1
{
JLabel n,accn,bal,age,sex,dep,pins;
JFrame X,r;
JTextField n1,accn1,bal1,sex1,age1,dep1;
JPasswordField pins1;
JLabel Xn,Xacc,Xbal,Xage,Xsex;
String xn,xacc,xbal,xage,xsex;
public void reader()
{
try
{
FileReader fr = new FileReader("DATABASE.TXT");
BufferedReader br = new BufferedReader(fr);
}
catch(FileNotFoundException s)
{
}
}
public Bank()
{
X = new JFrame("BANK");
X.setVisible(true);
X.setBounds(400,100,550,500);
X.setLayout(null);
x.setVisible(false);
JPanel info = new JPanel();
X.add(info);
info.setVisible(true);
info.setBounds(50,10,400,400);
info.setLayout(new GridLayout(7,2));
try
{
Scanner s2 = new Scanner(new File("DATABASE.txt"));
int loop2 = s2.nextInt();
for(int gkz=0;gkz<loop2;gkz++)
{
String info2[]= s2.nextLine().split(" ");
xn = info2[2];
xacc = info2[0];
xage = info2[3];
xsex = info2[4];
xbal =info2[5];
if(acc1.getText().equals(us))
{
if(pin1.getText().equals(pw))
{
JLabel Xn= new JLabel("NAME: "+xn);
JLabel Xacc = new JLabel("ACCOUNT NUMBER: "+xacc);
JLabel Xage = new JLabel("AGE: "+xage);
JLabel Xsex = new JLabel("SEX: "+sex);
JLabel Xbal = new JLabel("BALANCE: "+xbal);
}
}
}
}
catch(FileNotFoundException s)
{
}
}
public static void main(String args[])
{
}
public void actionPerformed(ActionEvent z1)
{
Object z2 = z1.getSource();
}
}
这是我得到的错误
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at Bank.<init>(Bank.java:54)
at Xavier1.GateKeeper(Xavier1.java:160)
at Xavier1.actionPerformed(Xavier1.java:108)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:1)
java.lang.ArrayIndexOutOfBoundsException: 2
(Bank.java:54)(Bank.java:54) -
xn = info2[2];
这是你的问题。说你的文件是这样的
3
data data data data
data data data data
下面的代码中的第一行将显示3
,但扫描仪将保留在该行上。
int loop2 = s2.nextInt();
for(int gkz=0;gkz<loop2;gkz++)
{
String info2[]= s2.nextLine().split(" ");
因此,对nextLine()
的下一次调用只会消耗行数并且只会将带到下一行,但实际上不会读取它。所以s2.nextLine().split(" ");
真的会返回一个空数组。因此,当您尝试执行info[2]
时,您会得到ArrayIndexOutOfBoundsException
,因为索引2不存在。
两种可能的修复方法。
s2.nextLine()
int loop2 = s2.nextInt();
就个人而言,我更喜欢总是按行读取(而不是nextInt / nextDouble)并解析它,所以我不会遇到你遇到的问题。
int loop2 = Integer.parseInt(s2.nextLine().trim());
for(int gkz=0;gkz<loop2;gkz++) {
...