Start New Discussion within our Software Development Community

Hello. Im doing some practice questions and Im kinda stuck. So basically I have three Jbuttons: "Menu", "Left" and "Right", a JTextField : "tDisplay" and an array : status.

code (dummy code for example):

String[] status = {"hello","goodbye","goodnight","goodmorning"};

what needs to happen is when Menu is clicked Hello is displayed in the textfield. when Right is clicked then Goodbye is displayed, clicked again Goodnight is displayed and so on and so forth. When Left is clicked if displayed text is on Goodnight then Goodbye is displayed and so on and so forth. The buttons sorts through the array.

What i have is:

if(e.getSource() == Menu){
    tDisplay.setText(status[0]);
}

if(e.getSource() == Left){
    for(int i = value;i<iterate; i--){
        tDisplay.setText(status[value--]);
    }
    iterate++;
}

if(e.getSource() == Right){
    for(int i = value;i<iterate; i++){
        tDisplay.setText(status[value++]);
    }
    iterate++;
}

of course you guys can tell theres a problem in the code. im not sure how to fix it. The Right button does sort through till Goodmorning but how do I stop or disable the Right button or something when it reaches Goodmorning? because if click again eclipse throws error.

OK, a couple of observations...

when you click menu you don't reset value
what is iterate all about???

to stop falling off the end of the array you just need to test for it, ie (pseudo code)

if value = last element in array: do nothing
else display array[++value]

ps: notice that you need to increment value before using it, not after