1

Hi so lately I've been doing a lot of java programming and I've been using a lot of if statements. the problem is i have to copy and paste the if statements over a hundred times to check all the senarios. Here is an example:

while (i < AreaNumbers.size()) {
        String roomnum = "j" + AreaNumbers.get(i);
        if (roomnum.equals("j100")) {
            if (k == 1) {
                j100.setVisible(true);
                j100.setToolTipText("<html>" + "<br>" + "Room Number: " + AreaNumbers.get(i) + "<br>" + "Incident ID: " + IncidentID.get(i) + "<br>" + " Teacher: " + Teachers.get(i) + "<br>" + " Description: " + Descriptions.get(i) + "</html>");
                k = k + 1;
            } else if (k > 0) {
                j100.setToolTipText(k + " help desk calls, click here for more information");
                k = k + 1;
            }

for this example i would copy and paste the if (roomnum.equals("j100")) { and everything past it for every label i want to check and compare. Is there anyway i could do this where i could write a statement that goes through this same scenario once and everywhere it sees j100 it could replace it after each time with a different label, such as j101 j107 an so on. Im sorry if this isnt very clear i just cant think of a better way to word it. Thanks in advance.

2
  • You have a variable named j100 and, according to your description, this code will be repeated over a hundred times. Does it mean you plan on having hundred variables like that? Commented Jan 25, 2011 at 0:01
  • so you would dynamic variables, like php? ${$name} Commented Jan 25, 2011 at 0:02

2 Answers 2

9

Don't use numbered variable names (like j100, j101, etc.). Use an array instead, so you can loop over them programatically:

SomeType[] j = new SomeType[1000];

for (int z = 0; z < j.length; z++) {
    j[z] = new SomeType();

    j[z].setTooltipText("Hello world");
}

Depending on what you need to do, you may want to investigate the more advanced collections classes instead of simple arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

I hope you never get to room -1
0

you could use a Map<String, Runnable> using the label as the key and to a Runnable. Depending on how you write you Runnable subclasses, this could alleviate a lot of redundancy.

if( myMap.containsKey( myLabel ) ) {
  myMap.get(myLabel).run();
}

6 Comments

I believe this approach is a bit too advanced for the question.
What do Runnables have to do with Threads besides the fact that a Thread can take a Runnable in the ctor? A Runnable is just an interface with a run method. Unlike c++ java does not have operator() for I used Runnable. This is the closest I can get to a functor in java6. You could use Callable or even create your own interface if you don't like Runnable.
As far as I understand it, the Runnable interface is designed for actions to be executed by a thread (it even says so in the documentation). It seems a bit perverse to implement this interface just to get a run() method, which you could quite easily do with a custom interface (that doesn't lead to confusion about threads!).
Why introduce a new interface that will almost certainly be exactly the same as Runnable? Runnable is the closest thing java currently has to a no-return lambda/functor. The interface Runnable has nothing intrinsically tying it to Thread.
You could also create your own interface with a go() or do() or execute() method. This way, you would not confuse those who are used to seeing Runnable in threading environments.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.