Where does the dead code come from? - java

I've got a problem, I'm getting a "Dead Code" warning in Eclipse and I really don't know why. The code is from my Connect Four project, to be more precise it's from the Class that checks if somebody has won. This method checks all the horizontal winning possibilities for red. The code is the following:
/**
* Method to check the horizontal winning possibilities for red
* #return true if red won or false if not
*/
public boolean checkHorRed(){
for(int line = 0; line < 6; line++) {
for(int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
if(gw.buttons[line][column].getIcon().equals(gw.red));
if(gw.buttons[line][column+1].getIcon().equals(gw.red));
if(gw.buttons[line][column+2].getIcon().equals(gw.red));
if(gw.buttons[line][column+3].getIcon().equals(gw.red));
return true;
}
}
return false;
}
The game is even caused to be immediately won because of this method. What's strange about this is that all the other methods in the class that look almost the same don't cause any problems. Here's the method that checks the vertical winning possibilities for yellow, to have a comparison:
/**
* Method to check the vertical winning possibilities for yellow
* #return true or false
*/
public boolean checkVertYel(){
for(int line = 3; line < 6; line++) {
for(int column = 0; column < 7; column++) {
if(gw.buttons[line][column].getIcon().equals(gw.yellow))
if(gw.buttons[line-1][column].getIcon().equals(gw.yellow))
if(gw.buttons[line-2][column].getIcon().equals(gw.yellow))
if(gw.buttons[line-3][column].getIcon().equals(gw.yellow))
return true;
}
}
return false;
}
This one does not cause any problems. Can somebody tell me where the warning comes from? If you need additional information please tell me.

The dead code in your function is the increment statement of your inner for loop (column++). The return true statement will always be executed (if the loop is executed), so the loop increment is never going to happen.
That is your code, but properly formatted:
// ...
for(int column = 0; column < 4; column++) {
//column++ is underlined and causes the "dead Code" warning
if(gw.buttons[line][column].getIcon().equals(gw.red));
if(gw.buttons[line][column+1].getIcon().equals(gw.red));
if(gw.buttons[line][column+2].getIcon().equals(gw.red));
if(gw.buttons[line][column+3].getIcon().equals(gw.red));
return true;
}
// ...
You can easily spot the error: return true will always be executed, so the increment statement of the inner loop will not be executed.
That is how you code should look like:
public boolean checkHorRed() {
for(int line = 0; line < 6; line++) {
for(int column = 0; column < 4; column++) {
//column++ is underlined and causes the "dead Code" warning
if(gw.buttons[line][column].getIcon().equals(gw.red)
&& gw.buttons[line][column+1].getIcon().equals(gw.red)
&& gw.buttons[line][column+2].getIcon().equals(gw.red)
&& gw.buttons[line][column+3].getIcon().equals(gw.red) {
return true;
}
}
}
return false;
}

This is your code after reformatting it:
public boolean checkHorRed() {
for (int line = 0; line < 6; line++) {
for (int column = 0; column < 4; column++) { //column++ is underlined and causes the "dead Code" warning
if (gw.buttons[line][column].getIcon().equals(gw.red)) {
;
}
if (gw.buttons[line][column + 1].getIcon().equals(gw.red)) {
;
}
if (gw.buttons[line][column + 2].getIcon().equals(gw.red)) {
;
}
if (gw.buttons[line][column + 3].getIcon().equals(gw.red)) {
;
}
return true; //this will always happen
}
}
return false;
}
And this is the other:
public boolean checkVertYel() {
for (int line = 3; line < 6; line++) {
for (int column = 0; column < 7; column++) {
if (gw.buttons[line][column].getIcon().equals(gw.yellow)) {
if (gw.buttons[line - 1][column].getIcon().equals(gw.yellow)) {
if (gw.buttons[line - 2][column].getIcon().equals(gw.yellow)) {
if (gw.buttons[line - 3][column].getIcon().equals(gw.yellow)) {
return true;
}
}
}
}
}
}
return false;
}
Basically, you should really not end your if statements with semicolons.

In the above method you have ; after each if statements, where as your second method is correct, which is proper way.
if(gw.buttons[line][column].getIcon().equals(gw.red)); <--
That terminates the if there it self. Your line of code equivalent to
if(condition)
{
}
Which means the code after the if conditions is dead.

Related

How to break and continue work in nested for loops? [duplicate]

I've got a nested loop construct like this:
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
break; // Breaks out of the inner loop
}
}
}
Now how can I break out of both loops? I've looked at similar questions, but none concerns Java specifically. I couldn't apply these solutions because most used gotos.
I don't want to put the inner loop in a different method.
I don't want to return the loops. When breaking I'm finished with the execution of the loop block.
Like other answerers, I'd definitely prefer to put the loops in a different method, at which point you can just return to stop iterating completely. This answer just shows how the requirements in the question can be met.
You can use break with a label for the outer loop. For example:
public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}
This prints:
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done
Technically the correct answer is to label the outer loop. In practice if you want to exit at any point inside an inner loop then you would be better off externalizing the code into a method (a static method if needs be) and then call it.
That would pay off for readability.
The code would become something like that:
private static String search(...)
{
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
return search;
}
}
}
return null;
}
Matching the example for the accepted answer:
public class Test {
public static void main(String[] args) {
loop();
System.out.println("Done");
}
public static void loop() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
return;
}
System.out.println(i + " " + j);
}
}
}
}
You can use a named block around the loops:
search: {
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
break search;
}
}
}
}
I never use labels. It seems like a bad practice to get into. Here's what I would do:
boolean finished = false;
for (int i = 0; i < 5 && !finished; i++) {
for (int j = 0; j < 5; j++) {
if (i * j > 6) {
finished = true;
break;
}
}
}
You can use labels:
label1:
for (int i = 0;;) {
for (int g = 0;;) {
break label1;
}
}
Use a function:
public void doSomething(List<Type> types, List<Type> types2){
for(Type t1 : types){
for (Type t : types2) {
if (some condition) {
// Do something and return...
return;
}
}
}
}
You can use a temporary variable:
boolean outerBreak = false;
for (Type type : types) {
if(outerBreak) break;
for (Type t : types2) {
if (some condition) {
// Do something and break...
outerBreak = true;
break; // Breaks out of the inner loop
}
}
}
Depending on your function, you can also exit/return from the inner loop:
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
return;
}
}
}
If you don't like breaks and gotos, you can use a "traditional" for loop instead the for-in, with an extra abort condition:
int a, b;
bool abort = false;
for (a = 0; a < 10 && !abort; a++) {
for (b = 0; b < 10 && !abort; b++) {
if (condition) {
doSomeThing();
abort = true;
}
}
}
Using 'break' keyword alone is not the appropriate way when you need to exit from more than one loops.
You can exit from immediate loop
No matter with how many loops your statement is surrounded with.
You can use 'break' with a label!
Here I've used the label "abc"
You can write your code as following, within any function in Java
This code shows how to exit from the most outer loop
abc:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
if (k == 1){
break abc;
}
}
}
}
Also you can use break statement to exit from any loop in a nested loop.
for (int i = 0; i < 10; i++) {
abc:for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
if (k == 1){
break abc;
}
}
}
}
The following code shows an example of exiting from the innermost loop.
In other works,after executing the following code, you are at the outside of the loop of 'k' variables and still inside the loop of 'j' and 'i' variables.
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
if (k == 1){
break;
}
}
}
}
I needed to do a similar thing, but I chose not to use the enhanced for loop to do it.
int s = type.size();
for (int i = 0; i < s; i++) {
for (int j = 0; j < t.size(); j++) {
if (condition) {
// do stuff after which you want
// to completely break out of both loops
s = 0; // enables the _main_ loop to terminate
break;
}
}
}
I prefer to add an explicit "exit" to the loop tests. It makes it clear to
any casual reader that the loop may terminate early.
boolean earlyExit = false;
for(int i = 0 ; i < 10 && !earlyExit; i++) {
for(int j = 0 ; i < 10 && !earlyExit; j++) { earlyExit = true; }
}
Java 8 Stream solution:
List<Type> types1 = ...
List<Type> types2 = ...
types1.stream()
.flatMap(type1 -> types2.stream().map(type2 -> new Type[]{type1, type2}))
.filter(types -> /**some condition**/)
.findFirst()
.ifPresent(types -> /**do something**/);
Labeled break concept is used to break out nested loops in java, by using labeled break you can break nesting of loops at any position.
Example 1:
loop1:
for(int i= 0; i<6; i++){
for(int j=0; j<5; j++){
if(i==3)
break loop1;
}
}
suppose there are 3 loops and you want to terminate the loop3:
Example 2:
loop3:
for(int i= 0; i<6; i++){
loop2:
for(int k= 0; k<6; k++){
loop1:
for(int j=0; j<5; j++){
if(i==3)
break loop3;
}
}
}
Usually in such cases, it is coming in scope of more meaningful logic, let's say some searching or manipulating over some of the iterated 'for'-objects in question, so I usually use the functional approach:
public Object searching(Object[] types) { // Or manipulating
List<Object> typesReferences = new ArrayList<Object>();
List<Object> typesReferences2 = new ArrayList<Object>();
for (Object type : typesReferences) {
Object o = getByCriterion(typesReferences2, type);
if(o != null) return o;
}
return null;
}
private Object getByCriterion(List<Object> typesReferences2, Object criterion) {
for (Object typeReference : typesReferences2) {
if(typeReference.equals(criterion)) {
// here comes other complex or specific logic || typeReference.equals(new Object())
return typeReference;
}
}
return null;
}
Major cons:
roughly twice more lines
more consumption of computing cycles, meaning it is slower from algorithmic point-of-view
more typing work
The pros:
the higher ratio to separation of concerns because of functional granularity
the higher ratio of re-usability and control of
searching/manipulating logic without
the methods are not long, thus they are more compact and easier to comprehend
higher ratio of readability
So it is just handling the case via a different approach.
Basically a question to the author of this question: what do you consider of this approach?
You can break from all loops without using any label: and flags.
It's just tricky solution.
Here condition1 is the condition which is used to break from loop K and J.
And condition2 is the condition which is used to break from loop K , J and I.
For example:
public class BreakTesting {
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
if (condition1) {
System.out.println("Breaking from Loop K and J");
k = 9;
j = 9;
}
if (condition2) {
System.out.println("Breaking from Loop K, J and I");
k = 9;
j = 9;
i = 9;
}
}
}
}
System.out.println("End of I , J , K");
}
}
Demo
public static void main(String[] args) {
outer:
while (true) {
while (true) {
break outer;
}
}
}
Best and Easy Method..
outerloop:
for(int i=0; i<10; i++){
// here we can break Outer loop by
break outerloop;
innerloop:
for(int i=0; i<10; i++){
// here we can break innerloop by
break innerloop;
}
}
Use Labels.
INNER:for(int j = 0; j < numbers.length; j++) {
System.out.println("Even number: " + i + ", break from INNER label");
break INNER;
}
Refer to this article
It's fairly easy to use label, You can break the outer loop from inner loop using the label, Consider the example below,
public class Breaking{
public static void main(String[] args) {
outerscope:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (condition) {
break outerscope;
}
}
}
}
}
Another approach is to use the breaking variable/flag to keep track of required break. consider the following example.
public class Breaking{
public static void main(String[] args) {
boolean isBreaking = false;
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (condition) {
isBreaking = true;
break;
}
}
if(isBreaking){
break;
}
}
}
}
However, I prefer using the first approach.
boolean broken = false; // declared outside of the loop for efficiency
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
broken = true;
break;
}
}
if (broken) {
break;
}
}
If it is inside some function why don't you just return it:
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
return value;
}
}
}
Rather unusual approach but in terms of code length (not performance) this is the easiest thing you could do:
for(int i = 0; i++; i < j) {
if(wanna exit) {
i = i + j; // if more nested, also add the
// maximum value for the other loops
}
}
Another one solution, mentioned without example (it actually works in prod code).
try {
for (Type type : types) {
for (Type t : types2) {
if (some condition #1) {
// Do something and break the loop.
throw new BreakLoopException();
}
}
}
}
catch (BreakLoopException e) {
// Do something on look breaking.
}
Of course BreakLoopException should be internal, private and accelerated with no-stack-trace:
private static class BreakLoopException extends Exception {
#Override
public StackTraceElement[] getStackTrace() {
return new StackTraceElement[0];
}
}
Demo for break, continue, and label:
Java keywords break and continue have a default value. It's the "nearest loop", and today, after a few years of using Java, I just got it!
It's seem used rare, but useful.
import org.junit.Test;
/**
* Created by cui on 17-5-4.
*/
public class BranchLabel {
#Test
public void test() {
System.out.println("testBreak");
testBreak();
System.out.println("testBreakLabel");
testBreakLabel();
System.out.println("testContinue");
testContinue();
System.out.println("testContinueLabel");
testContinueLabel();
}
/**
testBreak
a=0,b=0
a=0,b=1
a=1,b=0
a=1,b=1
a=2,b=0
a=2,b=1
a=3,b=0
a=3,b=1
a=4,b=0
a=4,b=1
*/
public void testBreak() {
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 5; b++) {
if (b == 2) {
break;
}
System.out.println("a=" + a + ",b=" + b);
}
}
}
/**
testContinue
a=0,b=0
a=0,b=1
a=0,b=3
a=0,b=4
a=1,b=0
a=1,b=1
a=1,b=3
a=1,b=4
a=2,b=0
a=2,b=1
a=2,b=3
a=2,b=4
a=3,b=0
a=3,b=1
a=3,b=3
a=3,b=4
a=4,b=0
a=4,b=1
a=4,b=3
a=4,b=4
*/
public void testContinue() {
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 5; b++) {
if (b == 2) {
continue;
}
System.out.println("a=" + a + ",b=" + b);
}
}
}
/**
testBreakLabel
a=0,b=0,c=0
a=0,b=0,c=1
* */
public void testBreakLabel() {
anyName:
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 5; b++) {
for (int c = 0; c < 5; c++) {
if (c == 2) {
break anyName;
}
System.out.println("a=" + a + ",b=" + b + ",c=" + c);
}
}
}
}
/**
testContinueLabel
a=0,b=0,c=0
a=0,b=0,c=1
a=1,b=0,c=0
a=1,b=0,c=1
a=2,b=0,c=0
a=2,b=0,c=1
a=3,b=0,c=0
a=3,b=0,c=1
a=4,b=0,c=0
a=4,b=0,c=1
*/
public void testContinueLabel() {
anyName:
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 5; b++) {
for (int c = 0; c < 5; c++) {
if (c == 2) {
continue anyName;
}
System.out.println("a=" + a + ",b=" + b + ",c=" + c);
}
}
}
}
}
for (int j = 0; j < 5; j++) //inner loop should be replaced with
for (int j = 0; j < 5 && !exitloops; j++).
Here, in this case complete nested loops should be exit if condition is True . But if we use exitloops only to the upper loop
for (int i = 0; i < 5 && !exitloops; i++) //upper loop
Then inner loop will continues, because there is no extra flag that notify this inner loop to exit.
Example : if i = 3 and j=2 then condition is false. But in next iteration of inner loop j=3 then condition (i*j) become 9 which is true but inner loop will be continue till j become 5.
So, it must use exitloops to the inner loops too.
boolean exitloops = false;
for (int i = 0; i < 5 && !exitloops; i++) { //here should exitloops as a Conditional Statement to get out from the loops if exitloops become true.
for (int j = 0; j < 5 && !exitloops; j++) { //here should also use exitloops as a Conditional Statement.
if (i * j > 6) {
exitloops = true;
System.out.println("Inner loop still Continues For i * j is => "+i*j);
break;
}
System.out.println(i*j);
}
}
Like #1800 INFORMATION suggestion, use the condition that breaks the inner loop as a condition on the outer loop:
boolean hasAccess = false;
for (int i = 0; i < x && hasAccess == false; i++){
for (int j = 0; j < y; j++){
if (condition == true){
hasAccess = true;
break;
}
}
}
Java does not have a goto feature like there is in C++. But still, goto is a reserved keyword in Java. They might implement it in the future. For your question, the answer is that there is something called label in Java to which you can apply a continue and break statement. Find the code below:
public static void main(String ...args) {
outerLoop: for(int i=0;i<10;i++) {
for(int j=10;j>0;j--) {
System.out.println(i+" "+j);
if(i==j) {
System.out.println("Condition Fulfilled");
break outerLoop;
}
}
}
System.out.println("Got out of the outer loop");
}
If it's a new implementation, you can try rewriting the logic as if-else_if-else statements.
while(keep_going) {
if(keep_going && condition_one_holds) {
// Code
}
if(keep_going && condition_two_holds) {
// Code
}
if(keep_going && condition_three_holds) {
// Code
}
if(keep_going && something_goes_really_bad) {
keep_going=false;
}
if(keep_going && condition_four_holds) {
// Code
}
if(keep_going && condition_five_holds) {
// Code
}
}
Otherwise you can try setting a flag when that special condition has occured and check for that flag in each of your loop-conditions.
something_bad_has_happened = false;
while(something is true && !something_bad_has_happened){
// Code, things happen
while(something else && !something_bad_has_happened){
// Lots of code, things happens
if(something happened){
-> Then control should be returned ->
something_bad_has_happened=true;
continue;
}
}
if(something_bad_has_happened) { // The things below will not be executed
continue;
}
// Other things may happen here as well, but they will not be executed
// once control is returned from the inner cycle.
}
HERE! So, while a simple break will not work, it can be made to work using continue.
If you are simply porting the logic from one programming language to Java and just want to get the thing working you can try using labels.
You just use label for breaking inner loops
public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}
You can do the following:
set a local variable to false
set that variable true in the first loop, when you want to break
then you can check in the outer loop, that whether the condition is set then break from the outer loop as well.
boolean isBreakNeeded = false;
for (int i = 0; i < some.length; i++) {
for (int j = 0; j < some.lengthasWell; j++) {
//want to set variable if (){
isBreakNeeded = true;
break;
}
if (isBreakNeeded) {
break; //will make you break from the outer loop as well
}
}

Filter the period in Char[]

The valid sudoku problem on Leetcode is solved, however, there is still a little question regarding the '.', which I fail to filter until I try another way.
Check out the comment parts regarding (1) & (2), (1) = the correct way to filter period by using continue; if '.' is found. (2) = is the wrong way which I used before, it will only allow digits to get passed for the following if statements.
Assume 1~9 digits and '.' will be the only inputs.
I just need someone to help me analyze the difference between these 2 ways, so I can learn from the mistakes.
Thank you for the help!
public class Solution {
public boolean isValidSudoku(char[][] board) {
if (board.length > 9 || board[0].length > 9 || board == null) {
return false;
}
boolean[] brain;
for (int x = 0; x < board.length; x++) {
// Reset brain
brain = new boolean[9];
for (int y = 0; y < board[0].length; y++) {
// ------- (1) Begin -------
if (board[x][y] == '.') {
continue;
}
if (brain[board[x][y] - '1']) {
return false;
} else {
brain[board[x][y] - '1'] = true;
}
// ------- (1) End -------
// statments (1) above is the correct one, I used to use code below:
/*
// ------- (2) Begin -------
if (board[x][y] != '.') {
if (brain[board[x][y] - '1']) {
return false;
} else {
brain[board[x][y] - '1'] = true;
}
}
// ------- (2) Begin -------
*/
// which failed for not filter out '.' properly
// so I changed to filter '.' out by using continue;
}
}
for (int x = 0; x < board.length; x++) {
// Reset brain
brain = new boolean[9];
for (int y = 0; y < board[0].length; y++) {
if (board[y][x] == '.') {
continue;
}
if (brain[board[y][x] - '1']) {
return false;
} else {
brain[board[y][x] - '1'] = true;
}
}
}
for (int block = 0; block < 9; block++) {
// Reset brain
brain = new boolean[9];
for (int r = block / 3 * 3; r < block / 3 * 3 + 3; r++) {
for (int c = block % 3 * 3; c < block % 3 * 3 + 3; c++) {
if (board[r][c] == '.') {
continue;
}
if (brain[board[r][c] - '1']) {
return false;
} else {
brain[board[r][c] - '1'] = true;
}
}
}
}
return true;
}
}
The two methods you use to skip dots on your Sudoku board are equivalent under the assumption that the digits 1 thru 9 and the dot are your only input.
I have tried to verify this with two different Sudoku boards, and both methods returned the same result. As I believe the two methods to do the same I think you will be hard pressed to find a board that shows differing results for each method.

Java Connect four method full board

okay so what I have to do is make a java connect four game. One of the methods which is named full is used to check if the board is full or not. If it is it returns true, if not then it returns false. White is used for empty spaces. The problem is I can't compare board and Color.White and I don't know what to do from here. My code is below
public static boolean full(Color[][] board) {
for(int i = 0; i < board.length; i++){
if (board != Color.WHITE){
return true;
} else {
return false;
}
}
}
It is hard to say with only the snippet of code. What kind of object is 'board'?
To me it seems like you should increment through the Color[][] array in a double for-loop and see if any of the elements are equal to Color.WHITE.
public static boolean full(Color[][] board) {
for(int i = 0; i < board.length; i++){
for(int j=0; j<board.length;j++) {
if (board[i][j] == Color.WHITE){
return false;
} else {
return true;
}
}
}
}
Do you have any logs, error messages, or print statements to help debug? Good luck!

Can you use a for loop inside the condition of an if-else statement?

Can you use a for loop inside the condition of an if-else statement? For example, something like this...
if(
for(q = 0; q < 10; q++){
values[q]>=values[q+1];
}
)
{
done = 0;
}
This is loading an error I can't seem to place. I want the if statement to check to see if the int[] I called values is in order from greatest to least, and if it is, set int variable done equal to 0.
I only just started taking a programming class and I bet this is a very dumb mistake, but I've been trying to figure this out for a while and some help would be absolutely fantastic.
You should work out your condition first (ie is your array in order), and then feed that in to your if statement. Like so...
boolean isOrdered = true;
for(q = 0; q < 10; q++){
if (values[q]>=values[q+1]){
// in order
}
else {
// not in order
isOrdered = false;
break; // we have found a false, so we can quit out of the for loop
}
}
if (isOrdered){
// do something if the array is in order;
}
You can do it if you refactor the logic into a method that returns a boolean:
if (isInOrder(values)) {
//
}
private static boolean isInOrder(int[] array) {
for (int i = 0; i < array.length - 1; i++)
if (array[i] > array[i+1])
return false;
return true;
}
A for loop can exist inside of an if block
if (true) {
for (int i = 0; i < 5; i++) {
System.out.println("Hello, World!");
}
}
But a for loop can not be the condition of the if block
if( for (int i = 0; i < 5; i++) { } ) { }
A for loop is not a boolean. Every if condition requires a boolean.
No. If requires a boolean test. For loop doesn't return a boolean value. You can instead separate the check into another function.
if( isArrayOrdered(values) ){
done = 0;
}
// assuming values is an array of `int` values
private boolean isArrayOrdered( int[] values){
for(int q = 1; q < values.length; q++){
if( values[q] > values[q-1] ){
return false;
}
}
return true;
}
I think you probably want the alternate nesting, and start with the assumption that done = 0 (that is, that the list is correctly ordered), unless you prove in the loop that it is not true.
public class Play{
public static void main(String[] args) {
done = 0;
for(q = 1; q < 10; q++){
if (values[q-1]<values[q]) {
done = 1;
break;
}
}
}
}
You need to break you logic down into steps.
first of all, a method
boolean isInOrder (int [] values) {
for(int q = 0; q < values.length - 1; q++){
if (values[q] > values[q+1]) {
return false;
}
}
return true;
}
// now lets call this method
if (isInOrder (values)) {
System.out.println ("In Order");
}
else {
System.out.println ("Not In Order");
}
No, you can't. The if condition must evaluate to some boolean value, which doesn't happen with this for loop.
It can only be in the if statement body, like
if(someCondition)
for(int i = 0;i < 10;i++)...
To achieve your goal, a code like this one might help:
boolean ordered = true;
for(int i = 0;i < array.length - 1;i++) {
if(array[i] > array[i+1]) {
ordered = false;
break;
}
}
if(ordered) // your if body here
It is impossible to put the "for loop" statement between if statement "(" and ")".But it is possible if you write "for loop" between if statement "{" and "}".Thanks
The expression inside the "if" condition should evaluate to a boolean or Boolean otherwise the code will not compile.
Check that Java specs page here

Strange Stack Overflow Error in Sudoko Backtracker

(Disclaimer: There are maybe 20 different versions of this question on SO, but a reading through most of them still hasn't solved my issue)
Hello all, (relatively) beginner programmer here. So I've been trying to build a Sudoku backtracker that will fill in an incomplete puzzle. It seems to works perfectly well even when 1-3 rows are completely empty (i.e. filled in with 0's), but when more boxes start emptying (specifically around the 7-8 column in the fourth row, where I stopped writing in numbers) I get a Stack Overflow Error. Here's the code:
import java.util.ArrayList;
import java.util.HashSet;
public class Sudoku
{
public static int[][] puzzle = new int[9][9];
public static int filledIn = 0;
public static ArrayList<Integer> blankBoxes = new ArrayList<Integer>();
public static int currentIndex = 0;
public static int runs = 0;
/**
* Main method.
*/
public static void main(String args[])
{
//Manual input of the numbers
int[] completedNumbers = {0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,3,4,
8,9,1,2,3,4,5,6,7,
3,4,5,6,7,8,9,1,2,
6,7,8,9,1,2,3,4,5,
9,1,2,3,4,5,6,7,8};
//Adds the numbers manually to the puzzle array
ArrayList<Integer> completeArray = new ArrayList<>();
for(Integer number : completedNumbers) {
completeArray.add(number);
}
int counter = 0;
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
puzzle[i][j] = completeArray.get(counter);
counter++;
}
}
//Adds all the blank boxes to an ArrayList.
//The index is stored as 10*i + j, which can be retrieved
// via modulo and integer division.
boolean containsEmpty = false;
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
if(puzzle[i][j] == 0) {
blankBoxes.add(10*i + j);
containsEmpty = true;
}
}
}
filler(blankBoxes.get(currentIndex));
}
/**
* A general method for testing whether an array contains a
* duplicate, via a (relatively inefficient) sort.
* #param testArray The int[] that is being tested for duplicates
* #return True if there are NO duplicate, false if there
* are ANY duplicates.
*/
public static boolean checkDupl(int[] testArray) {
for(int i = 0; i < 8; i++) {
int num = testArray[i];
for(int j = i + 1; j < 9; j++) {
if(num == testArray[j] && num != 0) {
return false;
}
}
}
return true;
}
/**
* If the puzzle is not full, the filler will be run. The filler is my attempt at a backtracker.
* It stores every (i,j) for which puzzle[i][j] == 0. It then adds 1 to it's value. If the value
* is already somewhere else, it adds another 1. If it is 9, and that's already there, it loops to
* 0, and the index beforehand is rechecked.
*/
public static void filler(int indexOfBlank) {
//If the current index is equal to the size of blankBoxes, meaning that we
//went through every index of blankBoxes, meaning the puzzle is full and correct.
runs++;
if(currentIndex == blankBoxes.size()) {
System.out.println("The puzzle is full!" + "\n");
for(int i = 0; i < 9; i++) {
System.out.println();
for(int j = 0; j < 9; j++) {
System.out.print(puzzle[i][j]);
}
}
System.out.println("\n" + "The filler method was run " + runs + " times");
return;
}
//Assuming the puzzle isn't full, find the row/column of the blankBoxes index.
int row = blankBoxes.get(currentIndex) / 10;
int column = blankBoxes.get(currentIndex) % 10;
//Adds one to the value of that box.
puzzle[row][column] = (puzzle[row][column] + 1);
//Just used as a breakpoint for a debugger.
if(row == 4 && column == 4){
int x = 0;
}
//If the value is 10, meaning it went through all the possible values:
if(puzzle[row][column] == 10) {
//Do filler() on the previous box
puzzle[row][column] = 0;
currentIndex--;
filler(currentIndex);
}
//If the number is 1-9, but there are duplicates:
else if(!(checkSingleRow(row) && checkSingleColumn(column) && checkSingleBox(row, column))) {
//Do filler() on the same box.
filler(currentIndex);
}
//If the number is 1-9, and there is no duplicate:
else {
currentIndex++;
filler(currentIndex);
}
}
/**
* Used to check if a single row has any duplicates or not. This is called by the
* filler method.
* #param row
* #return
*/
public static boolean checkSingleRow(int row) {
return checkDupl(puzzle[row]);
}
/**
* Used to check if a single column has any duplicates or not.
* filler method, as well as the checkColumns of the checker.
* #param column
* #return
*/
public static boolean checkSingleColumn(int column) {
int[] singleColumn = new int[9];
for(int i = 0; i < 9; i++) {
singleColumn[i] = puzzle[i][column];
}
return checkDupl(singleColumn);
}
public static boolean checkSingleBox(int row, int column) {
//Makes row and column be the first row and the first column of the box in which
//this specific cell appears. So, for example, the box at puzzle[3][7] will iterate
//through a box from rows 3-6 and columns 6-9 (exclusive).
row = (row / 3) * 3;
column = (column / 3) * 3;
//Iterates through the box
int[] newBox = new int[9];
int counter = 0;
for(int i = row; i < row + 3; i++) {
for(int j = row; j < row + 3; j++) {
newBox[counter] = puzzle[i][j];
counter++;
}
}
return checkDupl(newBox);
}
}
Why am I calling it a weird error? A few reasons:
The box that the error occurs on changes randomly (give or take a box).
The actual line of code that the error occurs on changes randomly (it seems to usually happen in the filler method, but that's probably just because that's the biggest one.
Different compilers have different errors in different boxes (probably related to 1)
What I assume is that I just wrote inefficient code, so though it's not an actual infinite recursion, it's bad enough to call a Stack Overflow Error. But if anyone that sees a glaring issue, I'd love to hear it. Thanks!
Your code is not backtracking. Backtracking implies return back on failure:
if(puzzle[row][column] == 10) {
puzzle[row][column] = 0;
currentIndex--;
filler(currentIndex);// but every fail you go deeper
}
There are must be something like:
public boolean backtrack(int currentIndex) {
if (NoBlankBoxes())
return true;
for (int i = 1; i <= 9; ++i) {
if (NoDuplicates()) {
puzzle[row][column] = i;
++currentIndex;
if (backtrack(currentIndex) == true) {
return true;
}
puzzle[row][column] = 0;
}
}
return false;
}

Categories

Resources