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){

if(e.getSource() == Left){
    for(int i = value;i<iterate; i--){

if(e.getSource() == Right){
    for(int i = value;i<iterate; i++){

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.

2 Years
Discussion Span
Last Post by JamesCherrill

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

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.