0
public void actionPerformed(ActionEvent e)
{
  int clicked = 0;
 if(arg == "Enter")
 {
     clicked++;
 }

 if (clicked == 0)
{
names[0] = nameField.getText();
numbers[0] = numberField.getText();
}

if( clicked == 1)
{
names[1] = nameField.getText();
numbers[1] = numberField.getText();
}

 if(arg == "Print")
 {
   String name = nameField.getText();
   String number = numberField.getText();
   JOptionPane.showMessageDialog(null,names[0] + numbers[0] + names[1] + numbers[1] + numbers[2] + names[2],"Info",JOptionPane.INFORMATION_MESSAGE);
 }

My program must take multiple names and numbers and be able to enter them into an array. After all of the data is entered, it must be able to be printed. I am having trouble under the Enter method because it continues to reset everytime instead of remaining constant. It only allows me to print the last typed name/number instead of saving all of the content. I am unsure of how to fix this and would be grateful for any suggestions at this point.

Jodie
  • 11
  • 5
  • You can probably streamline your code by using something in the lines of `names[++clicked] = nameField.getText();`, provided your array is of a size compatible with the potential number of clicks. Or use an `ArrayList`, which is equivalent to a resizeable array. Not very clear from your code though. – Mena Apr 25 '16 at 14:50
  • You're overriding the array anytime someone enters the data, – DCruz22 Apr 25 '16 at 14:51
  • @Mena last time I was told I added too much code, so I tried to narrow it down to my problem area. It only has to accept 10 values if that helps. I have to complete this project and the teacher gave us very little information so I'm looking for any help I can get. Thank you though – Jodie Apr 25 '16 at 14:52
  • @DCruz22 how could I fix that then? – Jodie Apr 25 '16 at 14:54
  • @Priyamal it's inside of the actionPerformed method, but not outside of that – Jodie Apr 25 '16 at 14:55
  • @Jodie as Mena suggested you can use an [ArrayList](http://www.tutorialspoint.com/java/java_arraylist_class.htm) to save the data entered. They will be added to a List without overriding it – DCruz22 Apr 25 '16 at 15:06
  • Side note: You should use ```.equals``` to compare strings and not ```==```. The latter only compares memory addresses, while the first compares the String's actual content. See [How do I compare strings in java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jorn Vernee Apr 25 '16 at 15:21

2 Answers2

1

You could start by moving int clicked out of this function.

Right now your actionPerformed function each time its called reset your clicked to 0 since you are setting it to 0 at the beggining of it.

public void actionPerformed(ActionEvent e)
{
    int clicked = 0;   //HERE is your problem
    if(arg == "Enter");
    ...

Making it a variable of class instead of function should help.

EDIT: You can do something like this:

int clicked = 0 
public void actionPerformed(ActionEvent e)
{
    if(arg == "Enter"){
         names[clicked] = nameField.getText();
         numbers[clicked] = numberField.getText();
         clicked++;
    }

As it was mentioned you could also use List, since it would save problems if you don't know how big of an array u need.

List<String> names = new ArrayList<String>();
List<String> numbers = new ArrayList<String>();

And in use

    if(arg == "Enter"){
         names.add(nameField.getText());
         numbers.add(numberField.getText());
    }
Paweł.Ch
  • 44
  • 4
  • Okay, so where would I begin to use my array to store the information? Everytime I ask the teacher he gives me a short answer and its unclear. I feel bad to continuing asking but I have no idea what to do. I've tried a billion things and it just continued to reset – Jodie Apr 25 '16 at 15:00
0

Instead of an array, you could use an ArrayList, it will allow you to add elements without having to supply an index.

Without givening away too much, like this:

ArrayList<String> names = new ArrayList<String>();
...
names.add("Johnny"); // Adds to the end of the list
names.add("Frank");

That way you don't need to keep the 'clicked' count.

You can use .get(i) to get the element at index i. Or just loop over the entire list using a for each loop:

for(String name : names) { // i.e. for each 'name' in 'names'
    System.out.println(name);
    // Or do something else with 'name'
}
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93