5

I've got a bit of an issue with my little program. I have a JOptionPane asking for a number, and if that number is less than 10, a loop that just continues on and on forever doing what's in it, keeping on asking for numbers. Inside that loop, I call a method, with an int as parameter. In the method, I need to (without altering any of the code in the class that calls the method) find out whether the number I entered is less than 1. If it is, I need to call on another method. That bit's done.

But! The mainloop keeps rolling, so it keeps doing the other stuff in the loop. I need to stop it from doing that, so in the if-statement in the method I need to break that specific iteration of the loop the method is in, and make it go on to a new iteration of the same loop, asking for a new number.

The first class (example):

number=Integer.parseInt( JOptionPane.showInputDialog( "bla bla" ) );
while (number !=- 10) {
  themethod(number);
  blah
  blah
  ...
}

The called method (example):

public void themethod(int number) {
  if (number<1) {
    call the other method
    break the iteration im in
  }
3
  • 1
    just return some error code and break the loop if that code is returned.
    – janoliver
    Dec 11, 2011 at 17:39
  • which loop you want to break? is there another loop inside theMethod?
    – fmucar
    Dec 11, 2011 at 17:39
  • It's impossible without altering the code in the while-loop. Which part is in which class, and which classes can't you change? Dec 11, 2011 at 17:43

4 Answers 4

7

There are a number of things you can do here. Ultimately what you do should depend on your coding style and what you are trying to accomplish.

Option 1 would be some variation of:

for (;;)
{
   int number = /* ... */;
   myMethod(number);
   if (number == -10)
      break;
}

You might say, rather subjectively and depending on circumstances, that this is bad, because knowledge of the termination condition is contained in the loop rather than the method doing the "real work". Maybe for your loop that's OK. Maybe in other circumstances (or perhaps with other programmers? This is very much a matter of taste.) you might want to make myMethod make that decision. In general my own personal taste usually leans towards not having scenario knowledge be distributed throughout various methods in source, but in one place.

So most of what I'll write from here on will be how to make myMethod make the decision about whether or not to terminate.

Option 2 - myMethod returns a boolean indicating we should terminate:

for (;;)
{
   int number = /* ... */;
   if (myMethod(number))
      break;
}

boolean myMethod(int number)
{
   // TODO - do stuff

   return number == -10;
}

But you might say that myMethod already wants to return some other type. I come from very much a C background so the idiom I'm most used to would be the "out parameter". Leading me to option 3:

Option 3 - Out parameter lets caller decide to terminate:

public class CancelIndicator
{
   public boolean shouldCancel;
};

CancelIndicator cancel = new CancelIndicator();

while (!cancel.shouldCancel)
{
   int number = /* ... */;
   myMethod(number, cancel);
}

int myMethod(int number, CancelIndicator cancel)
{
   // TODO - do stuff.

   cancel.shouldCancel = (number == -10);

   return /* ... */;
}

Or maybe you're more a fan of exceptions:

Option 3:

public class CancellationException extends Exception
{
}

try
{
   for (;;)
   {
      int number = /* ... */;
      myMethod(numberl);
   }
}
catch (CancellationException ex)
{
}

void myMethod(int number) throws CancellationException
{
   // TODO - do stuff.

   if (number == -10)
      throw new CancellationException();
}

As you can see there are a number of options. I'm sure one could spend a whole day talking about different ways to do it. Here is my sample of idioms I have seen - I'll warn you that it's been some time since I've done much in Java so I might not write the most idiomatic code here. :-)

4
  • I don't think that he can modify the contents of the loop, though, only the method. But great answer anyway. +1
    – eboix
    Dec 11, 2011 at 17:59
  • Thanks! When i saw the "throw new" thing i got how i was supposed to do this. Dec 11, 2011 at 18:00
  • Wait, so you could throw an exception all along? Is there some catch statement in the rest of the loop?
    – eboix
    Dec 11, 2011 at 18:04
  • 1
    I seem to have missed the part where you're not allowed to modify code. If you have some instructor telling you this, I think they approach education in a very silly way. It's like saying: nail this down but don't use a hammer.
    – asveikau
    Dec 11, 2011 at 18:08
3

Add a return value indicating that the while should break:

public boolean themethod(int number) {
   if(number<1) {
     call the other method
     return true;
   }
   return false;
}

Then:

while(number !=-10) {
  if(themethod(number)) break;
  bla
  bla
  ...
}

Edit: If you can't change the while code, throw an exception from the method:

public void themethod(int number) {
   if(number<1) {
     call the other method
     throw new RuntimeException("Negative Number");
   }
}
2
  • I cannot alter the code of the class where the loop is, neither change the type of the method :( Dec 11, 2011 at 17:40
  • @user1092483: What can you change then?
    – Tudor
    Dec 11, 2011 at 17:41
1

Wait, so did I get this straight? You have a loop that asks for a number, does something if the number is not -10, and otherwise breaks?

If so, look at this:

while(true) {
    number=Integer.parseInt( JOptionPane.showInputDialog( "bla bla" ) );
    if(number == -10) {
        break;
    }
    themethod(number);
}

Otherwise, if this is not the case, and you want to break in two cases, the -10 case and the method is false case, you can do this:

Change your method to return a boolean. If it's true, then it doesn't want to break. If it's false then it wants to break, then do this:

while(true) {
    number=Integer.parseInt( JOptionPane.showInputDialog( "bla bla" ) );
    if(number == -10) {
        break;
    }
    if(!themethod(number)) break;
}

If you really can't edit the loop, then just throw an exception from the method! That will exit your entire program, though. I don't see any other possible way of doing this, however.

4
  • Wait, and why can't you edit the class? Can you edit the conditions of the loop?
    – eboix
    Dec 11, 2011 at 17:44
  • Hmmm... then just call an exception. What have you been doing in the class. Maybe knowing that will give us a hint as to what they're expecting you to do.
    – eboix
    Dec 11, 2011 at 17:50
  • We have talket about exceptions, but when i try to add that it still doesnt work.. The problem is that when i enter a negative the methods after this method is still excecuted and they fail if i enter a negative number Dec 11, 2011 at 17:53
  • Maybe you could post all of your code. I have a feeling that there's more here that we can't see. The "bla"'s may hold the key to the answer.
    – eboix
    Dec 11, 2011 at 18:03
0

Without a return value for themethod(int) and without changing the other class's code, this isn't possible since as it is, there is no return communication. You'll have to change both; it can't be done without changing the logic in the loop.

1
  • 2
    Then there's no way to make a controlled break from the while loop; you only could do System.exit(0); which breaks off the whole program or throw an exception and hope the loop will deal with it. Maybe post your full code? Also, add a "homework" tag to your question please.
    – G. Bach
    Dec 11, 2011 at 17:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.