buttons and arrays
I am "very" new to java, so don't laugh at my programming (o.k., you laugh)....and for class I have written a basic program which creates a basic GUI. The GUI should have four buttons (first, next, previous, and last). They should work, but I can't seem to write the code correctly to iterate through the array. Can anyone help? Here is the part of the code that is handling this event:
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int current = 0;// define a current value
int maximum = 0;// start with the first value
for (int i = 0; i < Accounting.MAXIMUM_ITEMS; i++)
{
if (i < maximum)
{
maximum = i;// new maximum
current = i;
currnext = current++;
last = product.getName();
next = product[currnext].getName();
}
}
if (event.getActionCommand().equals("Last"))
{
System.out.println();
System.out.println("You have selected: " + last);
}
if (event.getActionCommand().equals("Next"))
{
System.out.println();
System.out.println("You have selected: " + next);
}
}
}
Thank you!
Please try posting again with code tags.
I will say that this looks suspicious to me
int current = 0; // define a current value
int maximum = 0; // start with the first value
for (int i = 0; i < Accounting.MAXIMUM_ITEMS; i++)
{
if (i < maximum)
Ok. I hope I do this right. Here goes nothing....
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int current = 0;// define a current value
int maximum = 0;// start with the first value
for (int i = 0; i < Accounting.MAXIMUM_ITEMS; i++)
{
if (i < maximum)
{
maximum = i;// new maximum
current = i;
currnext = current++;
last = product[i].getName();
next = product[currnext].getName();
}
}
if (event.getActionCommand().equals("Last"))
{
System.out.println();
System.out.println("You have selected: " + last);
}
if (event.getActionCommand().equals("Next"))
{
System.out.println();
System.out.println("You have selected: " + next);
}
}
}
Ok. So I changed to >. However, it still doesn't do what I want it to do. Right now if you click on Last, you do get the last element in the array, however, if you click on Next, you also get the last element in the array. I would presume that it would either fail since there are no more elements or that it would start over at the top. Am I wrong in this assumption?