Need to convert this for loop to a while loop - java

I solved a problem recently. But I have this one piece of code where I dont utilize the for loop initialization and condition check. It looks a bit odd that way for a for loop. I want to convert it into a while loop. Please help me do it. I tried many times, but somewhere something is missing.
for(;;current =(current+1)%n){
if(eliminated[current%n]){
continue;
}else{
inkiPinki++;
if(inkiPinki == m){
eliminated[current%n] = true;
printStatus(eliminated, people);
remainingGuys--;
break;
}
}
}
In the above code eliminiated[index] is a boolean.
Edit: Thanks to Geoff who provided me with a solution which I further minimized like this.
while( eliminated[current] || ++inkiPinki != m )
current = (current+1) % n;
eliminated[current] = true;
printStatus( eliminated, people );
remainingGuys--;

All for loops can be converted to while loops using the following pattern:
for (..xxx..; ..yyy..; ..zzz..) {
..aaa..
}
becomes
...xxx...
while (...yyy...) {
..aaa..
..zzz..
}
remember that
for (;;) {
..aaa..
}
is equivalent to
for (nop; true; nop) {
..aaa..
}
where "nop" means no operations.
In your example this makes your loop:
for(;;current =(current+1)%n){
if(eliminated[current%n]){
continue;
}else{
inkiPinki++;
if(inkiPinki == m){
eliminated[current%n] = true;
printStatus(eliminated, people);
remainingGuys--;
break;
}
}
}
equivalent to
// no initialzation needed
while(true) {
//if(eliminated[current%n]){
// continue;
//}else{
if(!eliminated[current%n]){
inkiPinki++;
if(inkiPinki == m){
eliminated[current%n] = true;
printStatus(eliminated, people);
remainingGuys--;
break;
}
}
current =(current+1)%n;
}
From there, you can simplify it further, if you wish.

Try
while( true ) {
if( !eliminated[current] ) {
if( ++inkiPinki == m ) {
break;
}
}
current = (current+1) % n;
}
eliminated[current] = true;
printStatus( eliminated, people );
remainingGuys--;
It should be logically equivalent.

How I would do it:
while (inkiPinki < m) {
if (!eliminated[current % n]) {
inkiPinki++;
if (inkiPinki == m) {
eliminated[current % n] = true;
}
}
if (inkiPinki < m) {
current = (current + 1) % n;
}
}
printStatus(eliminated, people);
remainingGuys--;
This code accomplishes exactly the same thing as your original for loop, except it uses logical tests to determine whether or not it should continue to loop. There's no need for continue or break. If you find yourself using either of these statements, there's probably some refactoring that should be done.

I seem to have an inordinate fondness for using Booleans as integers:
for (;inkiPinki<m; inkPinki += !eliminated[current])
current = (current + 1) %n;
eliminated[current] = true;
printStatus(eliminated, people);
remainingGuys--;
I've also changed current%n to simply current in a couple of places, because the %n is already done where current is incremented, so current should already be reduced modulo n.
If I were doing it, I'd probably change the sense, so instead of eliminated, it was something like remaining:
for (;inkiPinki<m; inkPinki += remaining[current])
current = (current + 1) %n;
remaining[current] = false;
printStatus(remaining, people);
remainingGuys--;

Related

For if loop wont loop: any pointers to why that might be?

This is supposed to loop 24 times; it does not, and I'm pretty confused as to why. Please help me various Kenobis out there :
private boolean simpleMove(Board bd)
{
int b = rn.nextInt(3);
for (int i = 0; i < 24; i++) {
if (bd.isVacant(i) && rul.isLegalMove(tigerLocs[b], i)) {
bd.swap(tigerLocs[b],i);
bd.setTiger(i);
tigerLocs[b] = i;
System.out.println(i);
return true;
}
else {
System.out.println(i);
}
}
System.out.println("invalid");
return false;
As the comments point out your loop will execute a maximum of 24 times.
But the return statement inside the if statement may cause it to return 'early'.
It looks like it's some kind of board game thing.
The board appears to have 24 'squares' and it makes the first legal move and returns true.
If it fails to find a legal move, it returns false.
I can't confirm the logic overall but that rationale seems sound:
If there's a move available, take it and return true.
If no move is available, make no move and return false.
If you expected it to continue, even after finding a "valid" move, then simply store the fact that a valid move has been found. This can be done in a separate boolean variable:
private boolean simpleMove(Board bd) {
int b = rn.nextInt(3);
boolean valid = false; // until proven otherwise below
for (int i = 0; i < 24; i++) {
if (bd.isVacant(i) && rul.isLegalMove(tigerLocs[b], i)) {
bd.swap(tigerLocs[b],i);
bd.setTiger(i);
tigerLocs[b] = i;
valid = true;
}
System.out.println(i); // why output HERE when we have a return value?
}
if (!valid) {
System.out.println("invalid"); // why output HERE when we have a return value?
}
return valid;
}
It's unclear if multiple "valid" moves could be found, and whether that would be a problem when you "swap" or not. If there is only ever one possible move, then there would be no need to continue iterating with the for loop; simply return in the body like you were doing.

Find bug in implementation of algorithm that finds minimum of an array of integers

Recently, I tried to write a Java program which searches for the minimum of an array.
I tried to write it in a different way, I know there are more simple ways to do that but I want to know why my program does not work.
Here is the source code :
public int minimum(int [] t) {
int min,i,j;
i=j=t.length/2;
min=t[t.length/2];
while(j!=0 || i!=t.length-1) {
while( t[i]>=min) {
i++;
if(i==t.length) {
i=t.length-1;
continue;
}
}
while(t[j]>=min) {
j--;
if(j==-1) {
j=0;
continue;
}
}
if(t[i]<=min && t[j]<=min) {
if(t[i]<=t[j]) min=t[i];
else min=t[j];
}
}
return min;
}
Thanks.
Before you read the answer you should try debugging your code to figure this out by yourself.
I think your code loops infinitely in one of those inner while loops because the end condition
if(i==t.length) {
i=t.length-1;
continue;
}
only resets the i one step back and the continue restarts the while loop. You probably meant to have the break keyword there instead of the continue in which case your code will continue with the other inner while loop.
there is some logic errors in my code , and it get infinitely going through the two loops , i fixed the loops by changing continue with break and i modify the last condition by setting || instead of && (that was a logic mistake), and it works now .
thanks guys.
here is the new source code:
public int minimum(int [] t) {
int min,i,j;
i=j=t.length/2;
min=t[t.length/2];
while(j!=0 || i!=t.length-1) {
while( t[i]>=min) {
i++;
if(i==t.length) {
i=t.length-1;
break;
}
}
while(t[j]>=min) {
j--;
if(j==-1) {
j=0;
break;
}
}
if(t[i]<=min || t[j]<=min) {
if(t[i]<=t[j]) min=t[i];
else min=t[j];
}
}
return min;
}

Which one is better, conditional for loop or for loop with a break (for short block of code)?

I am trying to find out which practices make your code more readable, specifically when your block of the code is short. Please look at these two versions of the for loop and let me know which one is more readable;
private boolean isValid(String guessInput)
{
boolean result = true;
for (int i = 0; i < guessInput.length(); i++)
{
if (!Character.isDigit(guessInput.charAt(i)))
{
result = false;
break;
}
}if (result)
{
int guessInputInt = Integer.parseInt(guessInput);
if (guessInputInt >= minGuess && guessInputInt < maxGuess)
guesses.add(guessInput);
else
result = false;
}else
System.out.println("Your input is not valid.");
return result;
}
Version 2
private boolean isValid(String guessInput)
{
boolean result = true;
for (int i = 0; i < guessInput.length() && result; i++)
{
if (!Character.isDigit(guessInput.charAt(i)))
{
result = false;
}
}if (result)
{
int guessInputInt = Integer.parseInt(guessInput);
if (guessInputInt >= minGuess && guessInputInt < maxGuess)
guesses.add(guessInput);
else
result = false;
}else
System.out.println("Your input is not valid.");
return result;
}
Approach to break the loop is better amongst the two. Also, you can move the Sysout statement inside the loop e.g.:
private boolean isValid(String guessInput){
for (int i = 0; i < guessInput.length() && result; i++){
if (!Character.isDigit(guessInput.charAt(i))){
return false;
}
}
return true;
Update
Here's another way to do it (using java 8 stream):
public static boolean isValid(String s){
return !s.chars()
.filter(c -> !Character.isDigit((char)c))
.findFirst()
.isPresent();
}
Agreed with Mark Jeronimus's point. You should call this method and print the messages in calling the method rather than having Sysout in the valildating method.
How about a third option:
private boolean isValid(String guessInput) {
boolean result = isNumeric(guessInput);
if (result)
// ...
}
private boolean isNumeric(String input) {
for (int i = 0; i < guessInput.length(); i++)
if (!Character.isDigit(guessInput.charAt(i)))
return false;
return !input.isEmpty();
}
Or even simpler:
private boolean isValid(String guessInput) {
try {
int guessInputInt = Integer.parseInt(guessInput);
if (guessInputInt >= minGuess && guessInputInt < maxGuess) {
guesses.add(guessInput);
return true;
}
} catch (NumberFormatException nfe) {
System.out.println("Your input is not valid.");
}
return false;
}
On a side note, the method name isValid() implies a read-only getter, which is very misleading when you're modifying state.
In your loops, if you can accurately specify your loop breaking condition in the for loop itself, that is always better.
I always think of break as a hack/patch/fix, never a solution.
Specifically to the question on the usage of break in the loop in your example, I don't think there is a difference in code readability, since it is so short.
However, in general I would prefer the break approach, because:
There is no reason to continue execution once the result is set to false. Depending on the length of "guessInput", and how the compiler may optimize your code, the code with break can be a lot more efficient.
Although the code inside for loop is short in your example, in practice it may likely to grow as the condition become more complex. By always breaking out of the loop where it should, it will become a lot easier follow the code logic when the code inside loop becomes longer or more complex.
Therefore, I would always prefer to use break where it should in a loop.
For this I like the use of
public boolean isValid(String s) {
if (isNum(s)) {
Integer num = Integer.parseInt(s);
return inRange(num, maxGuess, minGuess);
}
return false;
}
private boolean inRange(Integer i, Integer max, Integer min) {
return (i < max && i >= min);
}
private boolean isNum(String s) {
return s.chars().allMatch(Character::isDigit);
}
As others have mentioned System.out.println("Your input is not valid."); is best done by the consumer of the isValid function,
Breaking isValid into smaller operations helps with MURDER!
You can read about murder here
If your intention is to validate numbers, any numbers, then you'd use the API for that, and handle the exception if the number is invalid.
Caveat: In your code, if your string is over 2147483647, the exception is thrown anyway and unhandled, leading to bugs.
Here's a better version, although I'd still argue about splitting up responsibilities in a pure checking method and another method that adds it to the guesses and/or prints stuff.
private boolean isValid(String guessInput)
{
try {
int guessInputInt = Integer.parseInt(guessInput);
if (guessInputInt >= minGuess && guessInputInt < maxGuess) {
guesses.add(guessInput);
} catch (NumberFormatException ex) {
System.out.println("Your input is not valid.");
return false;
}
return true;
}

How to Make Program flow control jump back to a former loop in java?

So I have written a code that allows a user to find a word in a TextArea. I have nearly succeeded but for one thing. here, I will show you all the code and tell my problem.
if(ta.getText().length() != 0 && t1.getText().length() != 0)
{
char c1[] = ta.getText().trim().toCharArray();
char c2[] = t1.getText().trim().toCharArray();
for(int i=startFlag;i<c1.length;i++)
{
if(c1[i]==c2[0])
{
start = i;
break;
}
}
k=start;
for(int i=0;i<c2.length;i++)
{
if(c2[i] != c1[start++])
{
}
else
countFlag++;
}
if(countFlag==c2.length)
{
ta.select(k,c2.length);
startFlag++;
}
}
For reference, ta is the TextArea and t1 is the TextField where the user enters a word to find. i have a problem in the second for loop. What should I write in the if () block there so that whenever c2[i] != c1[start++] the control is shifted to the first for loop, that would again determine the value of start?
Create a method to get "start" that you can then call whenever you want.
if(ta.getText().length() != 0 && t1.getText().length() != 0)
{
char c1[] = ta.getText().trim().toCharArray();
char c2[] = t1.getText().trim().toCharArray();
k=getStart(startFlag, c1.length);
for(int i=0;i<c2.length;i++)
{
if(c2[i] != c1[start++])
{
start = getStart(startFlag, c1.length);
}
else
countFlag++;
}
if(countFlag==c2.length)
{
ta.select(k,c2.length);
startFlag++;
}
}
And getStart() is:
public int getStart(int startFlag, int length) {
for(int i=startFlag;i<length;i++)
{
if(c1[i]==c2[0])
{
return i;
}
}
}
You may need different inputs to getStart(), but hopefully this gets across the general idea.
The way your code is set up right now, what you're asking for is impossible. To do what you're asking, you'll need to refactor your current code into different methods. More specifically, you'll need to put the for loops into their own methods and then call these methods.
So what you would need to do is make a separate method for the for loop.
public static int startForLoop(int i) {
for(int i=startFlag;i<c1.length;i++)
{
if(c1[i]==c2[0])
{
start = i;
break;
}
}
}
Then you can just call startForLoop(0) initially and in the 2nd for loops if statment:
if(c2[i] != c1[start++])
{
startForLoop(start+1)
}
This will continue the for loop where it left off. If you need to run the 2nd for loop again then you have to make a separate method for it as well and basically place both of them in a while loop that continues till you find the result you want in the 2nd for loop.
May be this code piece help you what you are looking for.
Basically it moves along with the string to be searched in keeping in mind the index of the string to be searched for.
Sorry but i have implemented it in java, but the notion is same and the result returned is the best what i got.you must give it a try!
private static String searchString(String searchIN,String searchFOR){
if (searchFOR != "") {
int index = searchIN.toUpperCase().indexOf(searchFOR.toUpperCase());
String before = "";
String highlighted = "";
String after = "";
while (index >= 0) {
int len = searchFOR.length();
before = searchIN.substring(0, index);
highlighted = "\"" + searchFOR + "\"";//do what ever you want to do with searched string
after = searchIN.substring(index + len);
searchIN = before + highlighted + after;
index = searchIN.toUpperCase().indexOf(searchFOR.toUpperCase(), index + highlighted.length());
}
}
return searchIN;
}

trying to break out of for loop but keeps going back into it and performing recursive call

I just discovered the project euler website, I have done challenges 1 and 2 and have just started number 3 in java... here is my code so far:
import java.util.ArrayList;
public class IntegerFactorise {
private static int value = 13195;
private static ArrayList<Integer> primeFactors = new ArrayList<Integer>();
private static int maxPrime = 0;
/**
* Check whether a give number is prime or not
* return boolean
*/
public static boolean isPrimeNumber(double num) {
for(int i = 2; i < num; i++) {
if(num % i == 0) {
return false;
}
}
return true;
}
/*Multiply all of the prime factors in the list of prime factors*/
public static int multiplyPrimeFactors() {
int ans = 1;
for(Integer i : primeFactors) {
ans *= i;
}
return ans;
}
/*Find the maximum prime number in the list of prime numbers*/
public static void findMaxPrime() {
int max = 0;
for(Integer i : primeFactors) {
if(i > max) {
max = i;
}
}
maxPrime = max;;
}
/**
* Find all of the prime factors for a number given the first
* prime factor
*/
public static boolean findPrimeFactors(int num) {
for(int i = 2; i <= num; i++) {
if(isPrimeNumber(i) && num % i == 0 && i == num) {
//could not possibly go further
primeFactors.add(num);
break;
}
else if(isPrimeNumber(i) && num % i == 0) {
primeFactors.add(i);
findPrimeFactors(num / i);
}
}
int sumOfPrimes = multiplyPrimeFactors();
if(sumOfPrimes == value) {
return true;
}
else {
return false;
}
}
/*start here*/
public static void main(String[] args) {
boolean found = false;
for(int i = 2; i < value; i++) {
if(isPrimeNumber(i) && value % i == 0) {
primeFactors.add(i);
found = findPrimeFactors(value / i);
if(found == true) {
findMaxPrime();
System.out.println(maxPrime);
break;
}
}
}
}
}
I am not using the large number they ask me to use yet, I am testing my code with some smaller numbers, with 13195 (their example) i get down to 29 in this bit of my code:
else if(isPrimeNumber(i) && num % i == 0) {
primeFactors.add(i);
findPrimeFactors(num / i);
}
}
int sumOfPrimes = multiplyPrimeFactors();
if(sumOfPrimes == value) {
return true;
}
It gets to the break statement then finally the check and then the return statement.
I am expecting the program to go back to the main method after my return statement, but it jumps up to:
findPrimeFactors(num / i);
and tries to finish the iteration...I guess my understanding is a flawed here, could someone explain to me why it is behaving like this? I can't wait to finish it of :) I'll find a more efficient way of doing it after I know I can get this inefficient one working.
You are using recursion, which means that a function will call itself.
So, if we trace what your function calls are when you call return, we will have something like that:
IntegerFactorise.main()
|-> IntegerFactorise.findPrimeFactors(2639)
|-> IntegerFactorise.findPrimeFactors(377)
|-> IntegerFactorise.findPrimeFactors(29) -> return true;
So, when you return in the last findPrimeFactors(), you will only return from this call, not from all the stack of calls, and the execution of the previous findPrimeFactors() will continue just after the point where you called findPrimeFactors().
If you want to return from all the stack of calls, you have to modify your code to do something like that:
else if(isPrimeNumber(i) && num % i == 0) {
primeFactors.add(i);
return findPrimeFactors(num / i);
}
So that when the last findPrimeFactors() returns, all the previous findPrimeFactors() which called it will return too.
I think the problem is that you are ignoring the return value from your recursive call to findPrimeFactors().
Let's walk through this. We start with the initial call to findPrimeFactors that happens in main. We then enter the for loop as it's the first thing in that method. Now let's say at some point we get into the else statement and thus recursively call frindPrimeFactors(num / i). This will suspend the looping, but as this recursive call starts to run you enter the for loop again (remember, the previous loop is merely paused and not finished looping yet). This time around you encounter the break, which allows this recursive call to finish out, returning true of false. When that happens you are now back to the original loop. At this point the original loop continues even if the recursive call returned true. So, you might try something like this:
if (findPrimeFactors(num / i))
return true;
I'm assuming that you need to continue looping if the recursive call returned false. If you should always finish looping upon return (whether true or false) then try this:
return findPrimeFactors(num / i);

Categories