# Anyone know how to shorten this code. A for loop I think but how

I was just wondering if anyone could help me with this. I have the following piece of code that will have to be replicated 16 times and although it works fine in the context of the program it is very long and I was thinking is there a way to put it in a for loop. Note that Clap is a Choice() in an Applet so it is a string.So i can't just count down using a for(int i = 0;i < Clap.getSelectedIndex).
Could anyone please help with this. I know it's a stupid question, but my brain is just wrecked at the moment. Also currentLap is just an int. Anyway , here is the code.

if(Clap.getSelectedIndex() == 0)
currentLap = 1;
else if(Clap.getSelectedIndex() == 1)
currentLap = 2;
else if(Clap.getSelectedIndex() == 2)
currentLap = 3;
else if(Clap.getSelectedIndex() == 3)
currentLap = 4;
else if(Clap.getSelectedIndex() == 4)
currentLap = 5;
else if(Clap.getSelectedIndex() == 5)
currentLap = 6;
else if(Clap.getSelectedIndex() == 6)
currentLap = 7;
else if(Clap.getSelectedIndex() == 7)
currentLap = 8;
else if(Clap.getSelectedIndex() == 8)
currentLap = 9;
else if(Clap.getSelectedIndex() == 9)
currentLap = 10;
else if(Clap.getSelectedIndex() == 10)
currentLap = 11;
else if(Clap.getSelectedIndex() == 11)
currentLap = 12;
else if(Clap.getSelectedIndex() == 12)
currentLap = 13;
else
currentLap = 0;
###### Who is Participating?

Commented:
currentLap = Clap.getSelectedIndex() + 1;
if (currentLap<=0 || currentLap>13)
{
currentLap = 0;
}

0

Commented:
currentLap = ++Clap.getSelectedIndex();
0

Commented:
if (Clap.getSelectedIndex() > 12 || Clap.getSelectedIndex() < 0)
currentLap = 0;
else currentLap = Clap.getSelectedIndex() + 1;
0

Commented:
>>
if (currentLap<=0 || currentLap>13)
{
currentLap = 0;
}
>>

The first test contradicts the questioner's code does it not?:

>>if(Clap.getSelectedIndex() == 0)
currentLap = 1;

and the second is surely redundant?

>>if (Clap.getSelectedIndex() > 12

See above. Or am i missing something!?
0

Author Commented:
thanks very much
you saved me alot of paper
think of it as saving the environment
:)
0

Commented:
>>See above. Or am i missing something!?

Sorry - hang on - it's getting late here ;-)
0

Commented:
Gotta save dem trees :)
0

Commented:
Yes, disregard the below, but not the rest

The first test contradicts the questioner's code does it not?:

>>if(Clap.getSelectedIndex() == 0)
currentLap = 1;

johnome - what did you find wrong with my code?
0

Commented:
currentLap = Clap.getSelectedIndex() + 1;
if (currentLap<=0 || currentLap>13)
{
currentLap = 0;
}

This is not right, it should say:

currentLap = Clap.getSelectedIndex() + 1;
if (currentLap<=0 || currentLap>=13)
{
currentLap = 0;
}

bacause he increments the selected index... hmmm
0

Commented:
No... mabe it's just getting too late...
0

Commented:
cehj> what did you find wrong with my code?

I don't think it will compile. ++ operator requires a variable not a value.
Also assumes selected index between -1 and 12, though that assumption is probably valid.

0

Commented:
Can you tell me how currentLap could be > 13?

Mine won't compile! But i'm not sure why ;-)

currentLap = Clap.getSelectedIndex() + 1;

0

Commented:
>>++ operator requires a variable not a value.

Yes you're right - a surprise.

>>though that assumption is probably valid.

You're right there as well AFAIK
0

Author Commented:
it gives me an error saying it is looking for a value instead of an index Cehj
0

Commented:
Yes - see above.

>>
currentLap = Clap.getSelectedIndex() + 1;

>>
0

Commented:
> Can you tell me how currentLap could be > 13?

if selected index is >12. As I mentioned above it probably won't be, but nothing in the question states the bounds so added that just 2b sure.

> Mine won't compile! But i'm not sure why ;-)

++ operator requires a variable not a value.

0

Author Commented:
it gives me an error saying it is looking for a value instead of an index Cehj
0

Author Commented:
it gives me an error saying it is looking for a value instead of an index Cehj
0

Commented:
if((currentLap = Clap.getSelectedIndex()+ 1) < 1 || currentLap > 13)
currentLap = 0;

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.