如何创建一个存储输入信息的数组,然后打印该信息?

时间:2016-04-14 19:33:29

标签: java

 public class Chap extends Frame implements ActionListener
 {
   private Button keys[];
 private Panel keypad;
private Panel fields;
 private TextField nameField;
private TextField numberField;
private String name;
 private int number;
 private boolean clearText;
private boolean foundKey;
Button enterButton = new Button("Enter");
Button clearButton = new Button("Clear");
Button printButton = new Button("Print");
String names, numbers;
String ArrayValues[][] = new String[10][2];

  public Chap()
  {

enterButton.addActionListener(this);
clearButton.addActionListener(this);
printButton.addActionListener(this);

enterButton.setActionCommand("Enter");
clearButton.setActionCommand("Clear");
printButton.setActionCommand("Print");

// construct components and initialize beginning values
nameField = new TextField(20);
numberField = new TextField(20);
nameField.setEditable(true);
numberField.setEditable(false);
keypad = new Panel();
fields = new Panel();
keys = new Button[10];
number = 0;
name = "";
clearText = true;

fields.add(nameField);
fields.add(numberField);
fields.add(enterButton);
fields.add(clearButton);
fields.add(printButton);


public void actionPerformed(ActionEvent e)
{


 if(arg == "About")
{
  String message = "Program";
  JOptionPane.showMessageDialog(null,message,"About Program",JOptionPane.INFORMATION_MESSAGE);
} 

 if(arg == "Enter")
{
   for (int counter = 0; counter < 10; counter ++)
   {

   }

} 

 if(arg == "Print")
 {
   JOptionPane.showMessageDialog(null,ArrayValues,"Info",JOptionPane.INFORMATION_MESSAGE);
 }

我必须创建一个程序,最多可存储10个电话号码和姓名。一旦用户点击打印,就应显示所有存储的数据。我不确定如何将数据存储在数组中。名称字段是可编辑的,而数字字段只能通过数字键盘访问

2 个答案:

答案 0 :(得分:0)

您可以使用LinkedHashMap,这是一个示例:

LinkedHashMap<String, String> test = new LinkedHashMap<String, String>();
test.put("Dan", "867-5309");
test.put("Sam", "123-4567");
for(String key : test.keySet()) {
    System.out.println(key + "'s phone number is " + test.get(key));
}

将打印:

Dan's phone number is 867-5309
Sam's phone number is 123-4567

LinkedHashMap与纯粹的&#39;之间的区别HashMap是LinkedHashMap维护&#34;键的顺序&#34;当你把它们放进去时,HashMap完全忽略了这个命令。

答案 1 :(得分:0)

不确定你究竟是什么意思,但这可能有助于你获得逻辑:

String[] arr=new String[10] // to store 10 phone numbers

将元素添加到数组:

arr[i]=numberField.getText();

为每个动作事件设置“i”计数器。然后使用循环打印数组元素。