Link to home
Start Free TrialLog in
Avatar of johnome
johnome

asked on

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.
Thanks you in advance.

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;
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

currentLap = ++Clap.getSelectedIndex();
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if (Clap.getSelectedIndex() > 12 || Clap.getSelectedIndex() < 0)
  currentLap = 0;
else currentLap = Clap.getSelectedIndex() + 1;
>>
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!?
Avatar of johnome
johnome

ASKER

thanks very much
you saved me alot of paper
think of it as saving the environment
:)
>>See above. Or am i missing something!?

Sorry - hang on - it's getting late here ;-)
Gotta save dem trees :)
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?
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
No... mabe it's just getting too late...
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.
 
Can you tell me how currentLap could be > 13?

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

currentLap = Clap.getSelectedIndex() + 1;

should be quite adequate

>>++ 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
Avatar of johnome

ASKER

it gives me an error saying it is looking for a value instead of an index Cehj
Yes - see above.

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

should be quite adequate
>>
> 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.


Avatar of johnome

ASKER

it gives me an error saying it is looking for a value instead of an index Cehj
Avatar of johnome

ASKER

it gives me an error saying it is looking for a value instead of an index Cehj
if((currentLap = Clap.getSelectedIndex()+ 1) < 1 || currentLap > 13)
  currentLap = 0;