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
}
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. :-)
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");
}
}
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.
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.
Related
I know basic if statement structure, but this confuses me
public void setHandleName(String handle){
if(handle.length() < 10){
return;
}
handleName = handle;
}
It returns the value when the handle.length() is bigger than 10, isn't it supposed to be returning the value when handle.length() is less than 10?
Why does this if statement does the opposite?
if(false){ //do stuff; }
Where the if statement should be
if(true){ //do stuff; }
Sorry for the vague explanation, english is not my native language.
Any help will be appreciated!
Thanks!
It returns the value when the handle.length() is bigger than 10
No it doesn't. It never returns any value, the method is of type void. It does set the handleName instance field if the name is at least 10 characters long (and not set it if the name is less than 10 characters long).
, isn't it supposed to be returning the value when handle.length() is less than 10?
No, the if clearly says "If the length of handle is less than 10, return" and that's before it sets handleName. return leaves the function right away, bypassing any code that may follow.
Why does this if statement does the opposite?
if(false){ //do stuff; }
Because in that case, the logic is within the if block; in your first example, it's after an if block that returns early (bypassing the remainder of the function). That is, your first example is:
if (!condition) {
return;
}
doSomething();
but your second example is
if (condition) {
doSomething();
}
Here's an annotated version of your first example:
public void setHandleName(String handle){ // Accept a `handle` string parameter
if(handle.length() < 10){ // If the length of `handle` is less
// than 10, enter the block
return; // Leave this method immediately, without
// doing anything else
} // This is the end of the conditional bit
handleName = handle; // Set `handleName` to `handle`
}
So if we go into the if block, we'll return, and never reach the handleName = handle; line, so we never set it. If we don't go into the if block, we don't return early, so we do set it.
We can (and probably should) rewrite setHandleName to use the structure from your later examples:
public void setHandleName(String handle){ // Accept a `handle` string parameter
if(handle.length() >= 10){ // If `handle`'s length is 10 or higher
handleName = handle; // Set `handleName` to `handle`
} // This is the end of the conditional bit
}
It doesn't return anything (as the void return type indicates). It sets the value of handleName to handle, but only if handle.length() is greater or equal to 10. If handle.length() is less than 10 it will just return from the method without doing anything more.
There are a few things to note:
The return type on this method is void. This means that it doesn't have a value to return. You can absolutely use return in a void method, but that implies...
This method returns early based on a condition. Effectively, if the value doesn't satisfy a specific criteria, it silently ignores what was passed into it.
Does this make it clearer what the code is actually doing?
public void setHandleName(String handle){
if(handle.length() >= 10){
handleName = handle;
}
// otherwise handleName is not set, and the method exits
// nothing is returned because the method is 'void'
}
Please note that if(false){ //do stuff; } will never "do stuff"
This is essentially what i'm doing
while(true) //cant check for zero, there is more to do here
{
int val = 0;
for(SomeObject i : arrayListOfSomeObjects)
{
if(canDoSomething)
{
val++
}
}
if(val == 0)
{
break;
}
else
{
//do stuff with that nonzero value
}
}
It is strange, because the loop wont yield. As in, stop if unsuccessful. I have a proceedure that will keep going until it finishes, if it does not do anything (or does not meet my conditions in that operation) it wont change the value. Meaning that check on "val" should break the loop. But it doesn't. It just going. I have even printed "val" to the console, yet it returns 0.
At the end of that operation, even if the value IS 0, it keeps going!
Your code is correct.. Try the following..
int val = 1;
while(val>0)
{
//Some operation that will change the value IF successful
}
first int val = 0 supposed to be outside the loop because this will cause the value of the variable val always zero
You might need to make sure you are targeting the 'right' loop by adding a label:
label: while(true) //cant check for zero, there is more to do here
{
// ...
if(val == 0)
{
break label;
}
// ...
}
This will ensure that you break out of the while loop in case the check is in another loop or a switch statement. Otherwise, I am sure that something is causing the problem that you didn't show in your example. Beware of unwanted side-effects.
retry: {
.........
if(xyz < 5) {
continue retry;
}
}
Problem : continue cannot be used outside of a loop
Why iteration of a block is prevented in java?
It's not supported because noone implemented it, presumably because they thought that, well, loops should be implemented with the loop constructs Java provides.
What you want to do can easily be implemented as a while loop if you invert the condition and use break instead:
while (true) {
.........
if(xyz >= 5) {
break;
}
}
Or why not write it as a regular loop without break or continue?
do {
.........
} while (xyz < 5);
Problem : continue cannot be used outside of a loop
continue is used within a loop i.e. for/while/do-while, if statements are conditional code blocks and not loops.
Learn more about code branching statements here:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
The purpose of continue keyword is to skip rest of the code inside loop to start with the next pass of loop. So, by it's purpose, continue would not fit in the contexts outside the loops.
goto could be other option for you to reset (or restart) the code of execution. However the best way could be move such part into a method and use recursion along with return under specific conditions to achieve the purpose you are talking about.
As others have pointed out, labels in Java label loops, and are used to specify which loop to break or continue.
That said, if you really want to write "spaghetti code" you can misuse break and continue like for instance:
public class SillyGotoExmple {
public static void main(String args[]) {
int count, loops;
final int NUM_LOOPS=2;
final int MAX_COUNT=10;
loops=0;
bar: do {
count = 0;
foo: do {
System.out.println(count);
if(++count < MAX_COUNT) {
continue foo;
} else if(++loops < NUM_LOOPS) {
continue bar;
} else {
break bar;
}
} while (true);
} while(true);
}
}
For some more discussion on goto in Java, see e.g. Is there a goto statement in Java?
I have made a for loop with a nested if block. On exit of the loop I want the object to perform an action by calling a method. At the moment I am struggling to find how to do this. Could anyone make any suggestions?. Below is the code I have made.
public void goLoop()
{
for (int i = 1; i <= getAmountOfLoops(); i++ )
{
if (getPosition() => 25)
{
this.setPosition(4);
}
else
{
this.keepLooping();
this.setTotalAmountOfLoops(getTotalAmountOfLoops() + 2);
}
jump(); // Do I put the action to perform on exit of loop here?
}
Just call it after the loop:
for (... ) {
}
action();
BTW change getPosition() => 25 to getPosition() >= 25.
Note: It's always better to be aware of the logic of your program before you start coding, however if you start coding and suddenly encounter a trivial situation like this, just give it a try! and check if it produces the desired result or not.
I've heard that using while(true) is a bad programming practice.
So, I've written the following code to get some numbers from a user (with default values). However, if the user happens to type in -1, then it will quit the program for them.
How should this be written then without a while(true)? I can think of a condition to make the while loop go off that will get caught right away without continuing on until the next iteration?
Here is how I have it now:
public static void main(String[] args)
{
System.out.println("QuickSelect!");
while (true)
{
System.out.println("Enter \"-1\" to quit.");
int arraySize = 10;
System.out.print("Enter the size of the array (10): ");
String line = input.nextLine();
if (line.matches("\\d+"))
{
arraySize = Integer.valueOf(line);
}
if (arraySize == -1) break;
int k = 1;
System.out.print("Enter the kth smallest element you desire (1): ");
line = input.nextLine();
if (line.matches("\\d+"))
{
k = Integer.valueOf(k);
}
if (k == -1) break;
List<Integer> randomData = generateRandomData(arraySize, 1, 100);
quickSelect(randomData, k);
}
}
while (true) is fine. Keep it.
If you had a more natural termination condition, I'd say to use it, but in this case, as the other answers prove, getting rid of while (true) makes the code harder to understand.
There is a Single Entry Single Exit (SESE) school of thought that suggests that you should not use break, continue or abuse exceptions to do the same for some value of abuse). I believe the idea here is not that you should use some auxiliary flag variable, but to clearly state the postcondition of the loop. This makes it tractable to formerly reason about the loop. Obviously use the stands-to-reason form of reasoning, so it is unpopular with the unwashed masses (such as myself).
public static void main(String[] args) {
...
do {
...
if (arraySize == -1) {
...
if (k != -1) {
...
}
}
} while (arraySze == -1 || k == -1);
...
}
Real code would be more complex and you would naturally(!) separate out the inputing, outputting and core "business" logic, which would make it easier to see what is going on.
bool exit = false;
while (!exit) {
...
...
if (k == -1) {
exit = true;
}
else {
List <Integer> ....;
quickselect(.......);
}
}
But as has been said before, your while loop is a valid usage in this situation. The other options would simply build upon the if statements to check for the boolean and exit.
While having a loop like this is not technically wrong, some people will argue that it is not as readable as the following:
bool complete = false;
while (!complete)
{
if (arraySize == -1)
{
complete = true;
break;
}
}
Additionally, it is sometimes a good idea to have a safety loop counter that checks to make sure the loop has not gone through, say, 100 million iterations, or some number much larger than you would expect for the loop body. This is a secure way of making sure bugs don't cause your program to 'hang'. Instead, you can give the user a friendly "We're sorry but you've discovered a bug.. program will now quit.." where you set 'complete' to true and you end the program or do additional error handling. I've seen this in production code, and may or may not be something you would use.
while ( true ) is perfectly fine here, since the condition is really "while the user doesn't want to quit"!
Alternatively you could prompt for both the inputs on one line to simplify the logic, and use "q" for quit: this allows you to refactor the loop to "while ( !line.equals("q") )".
The problem is that you're doing an awful lot in that loop, rather than separating the functionality into simple methods.
If you want to stick to a procedural approach, you could move the reading of the array size and k into separate methods, and use the fact that the result of an assignment is the assigned value:
for (int arraySize; ( arraySize = readArraySize ( input ) ) != -1;) {
final int k = readKthSmallestElement ( input );
List<Integer> randomData = generateRandomData(arraySize, 1, 100);
quickSelect(randomData, k);
}
However that's still a bit ugly, and not well encapsulated. So instead of having the two != -1 tests on separate variables, encapsulate arraySize, k and randomData in an object, and create a method which reads the data from the input, and returns either a QuickSelect object or null if the user quits:
for ( QuickSelect select; ( select = readQuickSelect ( input ) ) != null; ) {
select.generateRandomData();
select.quickSelect();
}
You might even want to go to the next stage of creating a sequence of QuickSelect objects from the input, each of which encapsulate the data for one iteration:
for ( QuickSelect select : new QuickSelectReader ( input ) ) {
select.generateRandomData();
select.quickSelect();
}
where QuickSelectReader implements Iterable and the iterator has the logic to create a QuickSelect object which encapsulates arraySize, k, the list and the quick select operation. But that ends up being quite a lot more code than the procedural variants.
I'd only do that if I wanted to reuse it somewhere else; it's not worth the effort just to make main() pretty.
Also note that "-1" doesn't match the regex "\\d+", so you really do have an infinite loop.
If you really don't like while(true) you can always go for for(;;). I prefer the latter because it seems less redundant.