简体   繁体   中英

I am having trouble adding new elements to my array

I want to take the strings that are entered into the textfields and turn them into new elements of the array, but whenever I try to print them out, they still register as null. If you can try to ignore the stuff that is irrelated to the question, because I am experimenting with them, but I cannot add new elements to the array.

This is where the array is created and initialized.

public class AssignSeat {

String[] arrangement = new String[12];

public String[] SeatStart() {

    arrangement[0] = "Collins";
    arrangement[2] = "Faivre";
    arrangement[3] = "Kinnard";
    arrangement[6] = "Morgans";
    arrangement[7] = "Rohan";
    arrangement[8] = "Shatrov";
    arrangement[9] = "Sword";
    arrangement[11] = "Tuckness";

    System.out.format("%-15s%-15s%n", "seat", "passenger");

    for (int i=0; i<arrangement.length; i++) {
        System.out.format("%-15s%-15s%n", i+1, arrangement[i]);

    }
    System.out.println();
    return arrangement;

}


public String[] getArrangement() {
    return arrangement;
}

public void setArrangement(String[] arrangement) {
    this.arrangement = arrangement;
}
}

This is where I am trying to add the elements of the array. Specifically the second, fifth, sixth, and eleventh.

public void actionPerformed(ActionEvent event) {
    //String name = null;

    AssignSeat seat = new AssignSeat();
    seat.SeatStart();

    if(event.getSource() instanceof JButton){

        JButton clickedButton = (JButton) event.getSource();
        String buttonText = clickedButton.getText();


        if (buttonText.equals("2")) {
            entername.setVisible(true);
            seatnum.setVisible(true);
            confirmed.setVisible(true);
            inputline.setVisible(true);
            outputline.setVisible(true);
            inputline.setEditable(true);
            inputline.addKeyListener(new KeyAdapter() {
             public void keyPressed(KeyEvent e) {
                 int key = e.getKeyCode();
                 String name = null;

                 AssignSeat seat = new AssignSeat();
                 seat.SeatStart();

                 if (key == KeyEvent.VK_ENTER) {
                     Toolkit.getDefaultToolkit().beep();
                     name = inputline.getText();
                     seat.arrangement[1] = name;
                     System.out.println(seat.arrangement[1]);
                     inputline.setEditable(false);
                     outputline.setText("2");
                     two.setForeground(Color.black);
                     for( ActionListener al : two.getActionListeners() ) {
                         two.removeActionListener( al );
                     }                           

                 }
             }
        });
        } else if(buttonText.equals("5")) {
            entername.setVisible(true);
            seatnum.setVisible(true);
            confirmed.setVisible(true);
            inputline.setVisible(true);
            outputline.setVisible(true);
            inputline.setEditable(true);
            inputline.addKeyListener(new KeyAdapter() {
             public void keyPressed(KeyEvent e) {
                 int key = e.getKeyCode();
                 String name = null;

                 AssignSeat seat = new AssignSeat();
                 seat.SeatStart();

                 if (key == KeyEvent.VK_ENTER) {
                     Toolkit.getDefaultToolkit().beep();
                     name = inputline.getText();
                     seat.arrangement[4] = name;
                     System.out.println(seat.arrangement[4]);
                    inputline.setEditable(false);
                    outputline.setText("5");
                    five.setForeground(Color.black);
                    for( ActionListener al : five.getActionListeners() ) {
                        five.removeActionListener( al );
                    }
                 }
             }
        });
        } else if (buttonText.equals("6")) {
            entername.setVisible(true);
            seatnum.setVisible(true);
            confirmed.setVisible(true);
            inputline.setVisible(true);
            outputline.setVisible(true);
            inputline.setEditable(true);
            inputline.addKeyListener(new KeyAdapter() {
             public void keyPressed(KeyEvent e) {
                 int key = e.getKeyCode();
                 String name = null;

                 AssignSeat seat = new AssignSeat();
                 seat.SeatStart();

                 if (key == KeyEvent.VK_ENTER) {
                     Toolkit.getDefaultToolkit().beep();
                     name = inputline.getText();
                     seat.arrangement[5] = name;
                     System.out.println(seat.arrangement[5]);
                    inputline.setEditable(false);
                    outputline.setText("6");
                    six.setForeground(Color.black);
                    for( ActionListener al : six.getActionListeners() ) {
                         six.removeActionListener( al );
                     }
                 }
             }
        });
        } else if (buttonText.equals("11")) {
            entername.setVisible(true);
            seatnum.setVisible(true);
            confirmed.setVisible(true);
            inputline.setVisible(true);
            outputline.setVisible(true);
            inputline.setEditable(true);
            inputline.addKeyListener(new KeyAdapter() {
             public void keyPressed(KeyEvent e) {
                 int key = e.getKeyCode();
                 String name = null;

                 AssignSeat seat = new AssignSeat();
                 seat.SeatStart();

                 if (key == KeyEvent.VK_ENTER) {
                     Toolkit.getDefaultToolkit().beep();
                     name = inputline.getText();
                     seat.arrangement[10] = name;
                     System.out.println(seat.arrangement[10]);
                     inputline.setEditable(false);
                     outputline.setText("11");
                     eleven.setForeground(Color.black);
                     for( ActionListener al : eleven.getActionListeners() ) {
                         eleven.removeActionListener( al );
                     }
                 }
             }
        });
        }
    }

}

}

I'll take it you are fairly new to Java and programming in general judging from your code.

Take a look at Java Tutorials - Arrays to see how arrays work.

Now for your code, as Dheeraj said, you are creating a new seat object every time you click a button and then you call SeatStart() . That means that every change you have done to the array goes to waste. You probably cannot see that because in your GUI you are not checking the values of the array to populate whatever you have showing you the seats. So you only keep the last change you have made.

Move the print code in a new private method like printArray() so you can call it at your SeatStart() and when you have made changes so you don't have to write the same code again elsewhere.

You have repeating code that does the exact same thing. Think putting that in a same place and by changing one variable, you can have the same effect. That makes your code clearer and easier to maintain. You can do that either with an if or a while if you want something even more flexible.

I'll try to give you an overview. First, you don't instantiate the seat in the action listener but when the frame loads. The action listener has to do with the button you clicked, so it has no point being there.

public void actionPerformed(ActionEvent event) {
if(event.getSource() instanceof JButton){

    JButton clickedButton = (JButton) event.getSource();
    String buttonText = clickedButton.getText();

    if (buttonText.equals("2") || buttonText.equals("5") || buttonText.equals("6") || buttonText.equals("11")){
        int seatNum = Integer.parseInt(buttonText);     // you get the seat number into an integer.
                                                        //this only works if the text is actually representing a number
        entername.setVisible(true);
        seatnum.setVisible(true);
        confirmed.setVisible(true);
        inputline.setVisible(true);
        outputline.setVisible(true);
        inputline.setEditable(true);
        inputline.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();   // don't put code between this and if. 
                                            //No point doing something if the key is not enter

                if (key == KeyEvent.VK_ENTER) {             
                    Toolkit.getDefaultToolkit().beep();
                    String name = inputline.getText();
                    seat.arrangement[seatNum - 1] = name;
                    System.out.println(seat.arrangement[seatNum - 1]);
                    inputline.setEditable(false);
                    outputline.setText(buttonText);
                    clickedButton.setForeground(Color.black); // I'm guessing this is the button that got clicked?
                    for( ActionListener al : clickedButton.getActionListeners() ) {
                         clickedButton.removeActionListener( al );
                    }                           
                }
            }
        });
    }
}
}

Of course this just an approach and there are many ways to do it. Try keeping staff to where they belong. For instance those setVisible don't actually belong to the button's action listener. You could move them to a different method etc. You get the idea...

Hope I helped a bit :)

Edit 1. I just reviewed your code again. What are you trying to achieve with the KeyListener for enter? It's better to add the KeyListener where you are creating the component you want and not inside another actionListener. That would be awfully bad to debug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM