数组存储接口JAVA可能很容易解决

时间:2015-04-28 15:45:41

标签: java arrays interface

我有一个问题。我正在使用java applet设计一个界面,我想制作一个注册按钮,它接受用户名,密码,性别和年龄并将它们存储在一个数组中;从数组到 .txt 文件。该文件稍后将用于在用户尝试使用登录按钮登录时验证用户。

数组 似乎存在 问题,但我不知道出了什么问题。我在编程方面也很糟糕,所以也许对其他人来说这看起来很傻。不幸的是,我的声誉只有1,所以我不能发布界面的图片,但我认为你可以借助代码看到它。

我的代码太大了,无法发布到此处,所以我只会附加一个指向我的Netbeans文件夹的链接: CODE zip file

public class Interface extends javax.swing.JApplet {
private int year=2015;
private int year2=2015;
private int month=1;
private int month2=4;
private int day=1;
private int day2=28;
private int n;
private int age=0;
private String gender= "M";
private String[][] logData = new String[4][n];
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {                                      

    int v = jSlider1.getValue();
    String vv = Integer.toString(v);
    jTextField2.setText(vv);
}                                     

private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {                                      
    int v1 = jSlider2.getValue();
    String vv1 = Integer.toString(v1);
    jTextField4.setText(vv1);
}                                     

private void jToggleButton2StateChanged(javax.swing.event.ChangeEvent evt) {                                            
    boolean slct = jToggleButton2.isSelected();
    if (!slct)
        jTextField5.setText("ON");
    else
        jTextField5.setText("OFF");
}                                           

private void jSlider3StateChanged(javax.swing.event.ChangeEvent evt) {                                      
    int v2 = jSlider3.getValue();
    String vv2 = Integer.toString(v2);
    jTextField7.setText(vv2);
}                                     

private void jSlider4StateChanged(javax.swing.event.ChangeEvent evt) {                                      
    int v3 = jSlider4.getValue();
    String vv3 = Integer.toString(v3);
    jTextField9.setText(vv3);
}                                     

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JOptionPane.showMessageDialog(null, "The system will shut down");
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JOptionPane.showMessageDialog(null, "The system will restart");
}                                        

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
    int k = jComboBox1.getSelectedIndex();
    year=2015-k;
    jTextField3.setText(Integer.toString(year));
}                                           

private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
    int k = jComboBox2.getSelectedIndex();
    month = k+1;
    jTextField8.setText(Integer.toString(month));
}                                           

private void jComboBox3ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
    int k = jComboBox3.getSelectedIndex();
    day = k+1;

    switch (month){
        case 2:
            if (year%4==0){
                if (day>29)
                    day=29;
            }
            if (year%4!=0){
                if (day>28)
                    day=28;
            }
        case 4:
            if (day>30)
                day=30;
            break;
        case 6:
            if (day>30)
                day=30;
            break;
        case 9:
            if (day>30)
                day=30;
            break;
        case 11:
            if (day>30)
                day=30;
            break;
        default:
            break;
    }
    jTextField10.setText(Integer.toString(day));


}                                           

private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                           

private void jComboBox5ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
    int k = jComboBox5.getSelectedIndex();
    year2=2015-k;
    jTextField11.setText(Integer.toString(year2));
}                                           

private void jComboBox6ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
    int k = jComboBox6.getSelectedIndex();
    month2 = k+1;
    jTextField12.setText(Integer.toString(month2));
}                                           

private void jComboBox7ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
    int k = jComboBox7.getSelectedIndex();
    day2 = k+1;

    switch (month2){
        case 2:
            if (year2%4==0){
                if (day2>29)
                    day2=29;
            }
            if (year2%4!=0){
                if (day2>28)
                    day2=28;
            }
        case 4:
            if (day2>30)
                day2=30;
            break;
        case 6:
            if (day2>30)
                day2=30;
            break;
        case 9:
            if (day2>30)
                day2=30;
            break;
        case 11:
            if (day2>30)
                day2=30;
            break;
        default:
            break;
    }
    jTextField13.setText(Integer.toString(day2));
}                                           

private void jTextField11ActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
}                                            

private void jComboBox5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
}                                          

private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {                                       

    n++;
    if(month2<month)
        age=year2-year-1;
    if((month2==month)&&(day2<day))
        age=year2-year-1;
    if ((month2==month)&&(day2>=day))
        age=year2-year;
    if (month2>month)
        age=year2-year;
    String ageStr=Integer.toString(age);
    if(jComboBox4.getSelectedIndex()==1)
        gender="F";
    else
        gender="M";
    if ((jTextField6.getText().equalsIgnoreCase(""))&&(jPasswordField2.getText().equalsIgnoreCase("")))
        for (int i=0;i<n;i++){
            logData[0][i]=jTextField6.getText();
            logData[1][i]=jPasswordField2.getText();
            logData[2][i]=ageStr;
            logData[3][i]=gender;               
        }
    JOptionPane.showMessageDialog(null, "Registration successful");
    //System.out.println(logData[0][0]);
File log = new File("out.txt");
try{
if(log.exists()==false){
        log.createNewFile();
}
PrintWriter out = new PrintWriter(new FileWriter(log, true));
//out.println(logData[0][0]);
out.close();
}catch(IOException e){
    System.out.println("COULD NOT LOG!!");
}
}  

所以每当我尝试打印数组logData的值时,我都会遇到以下错误:

线程“AWT-EventQueue-1”中的异常java.lang.ArrayIndexOutOfBoundsException:0     在Interface.jButton4MouseReleased(Interface.java:1002)     在Interface.access $ 700(Interface.java:23)     在接口$ 8.mouseReleased(Interface.java:264)     在java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)     at java.awt.Component.processMouseEvent(Component.java:6525)     在javax.swing.JComponent.processMouseEvent(JComponent.java:3321)     at java.awt.Component.processEvent(Component.java:6290)     at java.awt.Container.processEvent(Container.java:2234)     at java.awt.Component.dispatchEventImpl(Component.java:4881)     at java.awt.Container.dispatchEventImpl(Container.java:2292)     at java.awt.Component.dispatchEvent(Component.java:4703)     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)     at java.awt.Container.dispatchEventImpl(Container.java:2278)     at java.awt.Component.dispatchEvent(Component.java:4703)     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)     at java.awt.EventQueue.access $ 400(EventQueue.java:97)     at java.awt.EventQueue $ 3.run(EventQueue.java:697)     at java.awt.EventQueue $ 3.run(EventQueue.java:691)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:86)     at java.awt.EventQueue $ 4.run(EventQueue.java:719)     at java.awt.EventQueue $ 4.run(EventQueue.java:717)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:75)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:716)     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:0)

由于您似乎总是处理相同数量的项目,为什么不使用Map而不是多维字符串数组?

Map vehicles = new HashMap();
vehicles.put("BMW", 5);
vehicles.put("Mercedes", 3);
vehicles.put("Audi", 4);
vehicles.put("Ford", 10);

Phonegap Build Documentation

或为你:

Map logData = new HashMap();
logData.put("jTextField6", jTextField6.getText());
logData.put("jPasswordField2", jPasswordField2.getText());
logData.put("age", ageStr);
logData.put("gender", gender);

然后你可以摆脱jButton4MouseReleased中那个尴尬的循环。如果你必须跟踪jButton4MouseReleased的调用,你可以使用这样的一系列尝试:

List attempts = new ArrayList();
Map logData = new HashMap();
logData.put("jTextField6", jTextField6.getText());
logData.put("jPasswordField2", jPasswordField2.getText());
logData.put("age", ageStr);
logData.put("gender", gender);
attempts.add(logData);