Why does this program never end?

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,627
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
I've been working on this program of mine for a month or so (it's a sudoku solver, I've been testing whether or not it's feasible to solve a sudoku puzzle using a brute force method)

Anyway after a couple of weeks of digging out bugs my program works flawlessly... Except for one aspect, that is. It never really terminates? I keep seeing "Running..." at the bottom of my screen, something none of my programs have ever done before.

This one was written in java. The general layout of it is something like this:

Code:
public class sudoku solver {
public static void main(String[] args) {
System.out.println("Sudoku solver V1");
System.out.println("Enter sudoku puzzle clues");
// User inputs some staff, bla bla bla
boolean puzzlesolved = false;
while (puzzlesolved == false) {
/* Code to solve
the puzzle
goes here */
if ( [TheConditionsForAValidSudokuSolution] = true) {
puzzlesolved = true;
}
}
System.out.println("Done.");
}
}

I know for a fact that the while loop stops executing (as it should, since solved is made true eventually), but it never outputs "Done.", my laptop is really warm, and the IDE I use still says it's running. I have to force quit the program.

Anyone know what it could be? I've checked everywhere for a missing closed bracket or semicolon or anything like that. Nada.
 

Youkai

Demon
Member
Joined
Jul 1, 2004
Messages
2,533
Trophies
1
Age
35
Location
Germany , NRW
XP
2,297
Country
Germany
hard to say when you can't or don't want to post the real code ^^V

in your example you made several typos which would cause a problem with the loop like
first you used "puzzlesolved" than you used "puzzle solved" and in the end only "solved" as your variable which won't work, but as you seem to be able to write a Programm I assume you are not as stupid as to make stuff like this wrong in the actually programm

I am not a Programmer so I only know the basics but yeah ;)
(I mostly used C++ and VB ... )



P.S. does it try to solve a Sudoku which has numbers inside already ? maybe the numbers in the Sudoku make in unsolvable ?
 

Arras

Well-Known Member
Member
Joined
Sep 14, 2010
Messages
6,314
Trophies
2
Age
28
XP
5,140
Country
Netherlands
Try just using the debugger/break points to check what exactly happens, I suppose. Just plonk down a break point after the loop to check whether it exits it properly and what happens afterwards, at least. The code thing you posted doesn't really help. It just leads to questions like "why would you set a flag to true, then immediately check whether it's false? just put the code inside the first if".
 

Lucifer666

all the world needs is me
OP
Member
Joined
Apr 22, 2011
Messages
1,627
Trophies
1
Location
The Fourth Dimension
XP
2,160
Country
United Kingdom
hard to say when you can't or don't want to post the real code ^^V

in your example you made several typos which would cause a problem with the loop like
first you used "puzzlesolved" than you used "puzzle solved" and in the end only "solved" as your variable which won't work, but as you seem to be able to write a Programm I assume you are not as stupid as to make stuff like this wrong in the actually programm

I am not a Programmer so I only know the basics but yeah ;)
(I mostly used C++ and VB ... )



P.S. does it try to solve a Sudoku which has numbers inside already ? maybe the numbers in the Sudoku make in unsolvable ?

yeah no I was typing that with my eyes shut while lying down on a sofa at college, just removed all those mistakes. no of course I didn't make those mistakes in my actual code :)

Try just using the debugger/break points to check what exactly happens, I suppose. Just plonk down a break point after the loop to check whether it exits it properly and what happens afterwards, at least. The code thing you posted doesn't really help. It just leads to questions like "why would you set a flag to true, then immediately check whether it's false? just put the code inside the first if".


you're right, for the sake of the example it's useless so i removed the redundancy. It makes sense inside the context of my actual code though.

I just posted it to show the general structure. basically I have a while loop that will continue as long as a boolean is false, but some of the code executed within said loop may make the boolean true, thereby presumably exiting the while loop. That's all I wanted to get across really

i'll put a break point when the boolean is assigned a value of true and see what happens. thanks. weird issue huh
 

Reiten

Well-Known Member
Newcomer
Joined
Feb 28, 2014
Messages
80
Trophies
0
XP
1,969
Country
Germany
The only reason I can see for the loop never ending is that this part never evaluates to true(also there is a typo there. Assuming that in the real code you have == and not = in the if statement):
Code:
if ( [TheConditionsForAValidSudokuSolution] = true) {
puzzlesolved = true;
}
No idea how you do the checks and what puzzle you use, so can't help with that.
I'd also suggest you use a do{}while loop, but that's not really important.
 

TVL

#|
Member
Joined
Feb 17, 2004
Messages
575
Trophies
1
Location
World -1
XP
2,096
Country
Sweden
"if ( [TheConditionsForAValidSudokuSolution] = true)"

That line sets [TheConditionsForAValidSudokuSolution] to true.

Use == to actually test and not set it.


Edit: But that would make the loop exit right away... I actually tried it and I do get to see "Done." written out. Putting a bool in place of [TheConditionsForAValidSudokuSolution]. I had to look if maybe you had your statement outside of main... but it isn't, so I have no idea, have you run that code just as you posted it? You have probably edited out where it all goes wrong.

If I do change it to == It's locked up in an infinite loop though. That's how you know there's no solution to the sudoko "yeah, the program just hangs and you have to force quit.". Making something userfriendly is just a big waste of time. ;)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • JuanMena @ JuanMena:
    Will you give me mouth to mouth oxygen if my throat closes?
  • K3N1 @ K3N1:
    Nah the air can do that
  • K3N1 @ K3N1:
    Ask @x65943 he's trained for that stuff
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
    A @ abraarukuk: hi guys