?
Solved

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

Posted on 2003-03-10
19
Medium Priority
?
169 Views
Last Modified: 2010-03-31
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;
0
Comment
Question by:johnome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 4
  • +2
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8106968
currentLap = ++Clap.getSelectedIndex();
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 8106990
currentLap = Clap.getSelectedIndex() + 1;
if (currentLap<=0 || currentLap>13)
{
   currentLap = 0;
}

0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8107004
if (Clap.getSelectedIndex() > 12 || Clap.getSelectedIndex() < 0)
  currentLap = 0;
else currentLap = Clap.getSelectedIndex() + 1;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8107071
>>
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 Comment

by:johnome
ID: 8107075
thanks very much
you saved me alot of paper
think of it as saving the environment
:)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8107079
>>See above. Or am i missing something!?

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

Expert Comment

by:objects
ID: 8107085
Gotta save dem trees :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8107098
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8107105
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8107116
No... mabe it's just getting too late...
0
 
LVL 92

Expert Comment

by:objects
ID: 8107153
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8107155
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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8107180
>>++ 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 Comment

by:johnome
ID: 8107186
it gives me an error saying it is looking for a value instead of an index Cehj
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8107196
Yes - see above.

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

should be quite adequate
>>
0
 
LVL 92

Expert Comment

by:objects
ID: 8107206
> 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 Comment

by:johnome
ID: 8107223
it gives me an error saying it is looking for a value instead of an index Cehj
0
 

Author Comment

by:johnome
ID: 8107278
it gives me an error saying it is looking for a value instead of an index Cehj
0
 

Expert Comment

by:achirinos
ID: 8114775
if((currentLap = Clap.getSelectedIndex()+ 1) < 1 || currentLap > 13)
  currentLap = 0;

0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question