Strange Problem
Any ideas whats happening here with my program
I compile it, run it... it works fine...close it
run it.... it doesn't work
compile it again and run it..... not working
alter the code slightly by placing something pointless like // after a command and save it then compile it..... it works
[320 byte] By [
emdiessea] at [2007-11-27 4:56:13]

Let's see now. We're supposed to know why your code is not working without seeing your code. hm... I have many wonderful attributes including good looks and personality, but omnipotence and omniscience aren't among them (nor modesty).
try posting some code (with tags), and you'll get a less sarcastic reply. Promise.
How are you compiling it?How are you running it?
I just wondered if it was a common problem and if there was a common solution. Hence the reason I have not included code.
I have decided to change my approach to the problem and have undone most of my changes.
I am compiling my program using the javac command in cmd.exe and running it using the java command.
Anyway. I have a new problem now. I understand how you might not be omnipotent so I have included code this time.
My problem is my program freezes randomly.
It is a sudoku generator/solver.
For solving I just click generate to make a solution and then reset and generate again for a new one. However the program randomly freezes. Sometimes I can make up to 10 solutions before it freezes, other times only about 3
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Sudoku extends JFrame
implements ActionListener
{
static Numbers One= new Numbers(1);
static Numbers Two= new Numbers(2);
static Numbers Three = new Numbers(3);
static Numbers Four= new Numbers(4);
static Numbers Five= new Numbers(5);
static Numbers Six= new Numbers(6);
static Numbers Seven = new Numbers(7);
static Numbers Eight = new Numbers(8);
static Numbers Nine= new Numbers(9);
static Strings Possibilities = new Strings(); //Must be below Numbers declarations
static Solution Final= new Solution();
static Solution Initial= new Solution();
JButton btnOption = new JButton("Generate");
JButton[][]btn = new JButton[9][9];
public Sudoku()
{
super("Sudoku");
setSize(250, 290);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
setVisible(true);
FlowLayout flow = new FlowLayout();
pane.setLayout(flow);
flow.setHgap(0); flow.setVgap(0);
for(int r=0;r<9; r++)
{
for(int c=0;c<9; c++)
{
btn[r][c] = new JButton();
btn[r][c].setMargin(new Insets(0,0,0,0));
btn[r][c].setPreferredSize(new Dimension(25,25));
btn[r][c].setText("0");
btn[r][c].setFocusPainted(false);
btn[r][c].addActionListener(this);
btn[r][c].setActionCommand(Integer.toString(r)+Integer.toString(c));
pane.add(btn[r][c]);
}
}
btnOption.addActionListener(this);
btnOption.setFocusPainted(false);
pane.add(btnOption);
setContentPane(pane);
}
public static void main(String[] args)
{
Sudoku mainFrame = new Sudoku();
}
public void actionPerformed(ActionEvent e)
{
JButton buttonPressed = (JButton)e.getSource(); //Identify button pressed
int btnRow=-1, btnCol=-1;
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
if(buttonPressed.equals(btn[row][col]))
{
btnRow = row; btnCol=col;
}
}
}
if(btnRow!=-1 && btnCol!=-1)
{
String buttonText = btn[btnRow][btnCol].getText();
int buttonInt = Integer.parseInt(buttonText);
if(buttonInt <9) buttonInt += 1;
else buttonInt = 0;
buttonText = Integer.toString(buttonInt);
btn[btnRow][btnCol].setText(buttonText);
}
else if ("Generate".equals(e.getActionCommand()))
{
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
Initial.Table[row][col]=Integer.parseInt(btn[row][col].getText());
}
}
Final.setTable();
Final.Generate();
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
btn[row][col].setText(Integer.toString(Final.Table[row][col]));
}
}
btnOption.setText("Reset");
btnOption.setActionCommand("Reset");
btnOption.setToolTipText("Click this button to reset");
}
else if("Reset".equals(e.getActionCommand()))
{
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
btn[row][col].setText("0");
}
}
btnOption.setText("Generate");
btnOption.setActionCommand("Generate");
btnOption.setToolTipText("Click this button to generate a solution");
Initial.ZeroTable();
restart();
}
}
static void Update()
{
One.Update(1);
Two.Update(2);
Three.Update(3);
Four.Update(4);
Five.Update(5);
Six.Update(6);
Seven.Update(7);
Eight.Update(8);
Nine.Update(9);
Possibilities.Update();
}
static void restart()
{
Sudoku.One.Initialise(1);
Sudoku.Two.Initialise(2);
Sudoku.Three.Initialise(3);
Sudoku.Four.Initialise(4);
Sudoku.Five.Initialise(5);
Sudoku.Six.Initialise(6);
Sudoku.Seven.Initialise(7);
Sudoku.Eight.Initialise(8);
Sudoku.Nine.Initialise(9);
Sudoku.Final.setTable();
Output="";
}
}
class Numbers
{
int[][]Table = new int[9][9];
public Numbers(int value)
{
Initialise(value);
}
void Initialise(int value)
{
for(int row=0; row<9; row++)//Nested for loops used
{//to address each cell
for(int col=0; col<9; col++)//of the objects table
{
Table[row][col]=value;
}
}
}
void Update(int value)//Update the strings table to show which values are still available for the puzzle
{
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
boolean rowcon;
boolean colcon;
if(Sudoku.Final.Table[row][col]>0)Table[row][col] = 0;
rowcon = Sudoku.Final.getRow(row, value);//Check the current row for the table value
if(rowcon==true) Table[row][col] = 0;
colcon = Sudoku.Final.getCol(col, value);//Check the current col for the table value
if(colcon==true) Table[row][col] = 0;
boolean square = Sudoku.Final.getSquare(row, col, value); //Check the square the cell is in for the table value
if(square==true) Table[row][col] = 0;
}
}
}
void Zero(int row, int col) //Redundant, but can be used to speed up processing
{
Table[row][col] = 0;
}
}
class Strings
{
static String[][]Table = new String[9][9];
String One, Two, Three, Four, Five, Six, Seven, Eight, Nine;
public Strings()
{
Update();
}
void Update()
{
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
if(Sudoku.One.Table[row][col]==1)One="1"; else One="";
if(Sudoku.Two.Table[row][col]==2)Two="2"; else Two="";
if(Sudoku.Three.Table[row][col]==3)Three="3"; else Three="";
if(Sudoku.Four.Table[row][col]==4)Four="4"; else Four="";
if(Sudoku.Five.Table[row][col]==5)Five="5"; else Five="";
if(Sudoku.Six.Table[row][col]==6)Six="6"; else Six="";
if(Sudoku.Seven.Table[row][col]==7)Seven="7"; else Seven="";
if(Sudoku.Eight.Table[row][col]==8)Eight="8"; else Eight="";
if(Sudoku.Nine.Table[row][col]==9)Nine="9"; else Nine="";
Table[row][col] = One+Two+Three+Four+Five+Six+Seven+Eight+Nine;
}
}
}
}
class Solution
{
static Random generator = new Random();
int[][]Table = new int[9][9];
public Solution()
{
ZeroTable();
}
void setTable()
{
Table=Sudoku.Initial.Table;
Sudoku.Update();
}
void ZeroTable()
{
for(int row=0;row<9;row++)
{
for(int col=0; col<9; col++)
{
Table[row][col]=0;
}
}
}
void Generate()
{
while(containsZeros()==true)
{
genCell();
Sudoku.Update();
}
}
private void genCell()
{
int row=0, col=0, cellLength=9;
for(int r=0; r<9; r++) //Following for loops used to choose the cell with the minimum possibilities
{
for(int c=0; c<9; c++)
{
if(Sudoku.Possibilities.Table[r][c].length()<cellLength)
{
if(Sudoku.Possibilities.Table[r][c].length()==0){}
else
{
cellLength = Sudoku.Possibilities.Table[r][c].length();
row = r;
col = c;
}
}
}
}
int value=0, index=0, length=Sudoku.Possibilities.Table[row][col].length();
if(length>0)
{
index=generator.nextInt(length);
value = Sudoku.Possibilities.Table[row][col].charAt(index);//This returns the ascii code of the char at point index
}
else
{
Sudoku.restart();
}
//Convert the ascii code to its char equivelent but as an integer using these else if statements
if(value=='1')value=1;
else if(value=='2')value=2;
else if(value=='3')value=3;
else if(value=='4')value=4;
else if(value=='5')value=5;
else if(value=='6')value=6;
else if(value=='7')value=7;
else if(value=='8')value=8;
else if(value=='9')value=9;
Table[row][col]=value;
}
private boolean containsZeros()
{
boolean contains=false;
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
if(Table[row][col]==0)
{
contains=true;
break;
}
}
if(contains==true) break;
}
return(contains);
}
boolean getRow(int row, int value)
{
boolean contains=false, end=false;
int col=0;
while(contains==false && end==false)
{
if(Sudoku.Final.Table[row][col]==value) contains = true;
else contains = false;
col++;
if(col>8) end=true;
}
return(contains);
}
boolean getCol(int col, int value)
{
boolean contains=false, end=false;
int row=0;
while(contains==false && end==false)
{
if(Sudoku.Final.Table[row][col]==value) contains = true;
row++;
if(row>8) end=true;
}
return(contains);
}
boolean getSquare(int row, int col, int value)
{
boolean contains=false;
if(row==0 || row==1 || row==2)
{
if(col==0 || col==1 || col==2)
{
contains=checkSquare(0, 0, value);
}
if(col==3 || col==4 || col==5)
{
contains=checkSquare(0, 3, value);
}
if(col==6 || col==7 || col==8)
{
contains=checkSquare(0, 6, value);
}
}
if(row==3 || row==4 || row==5)
{
if(col==0 || col==1 || col==2)
{
contains=checkSquare(3, 0, value);
}
if(col==3 || col==4 || col==5)
{
contains=checkSquare(3, 3, value);
}
if(col==6 || col==7 || col==8)
{
contains=checkSquare(3, 6, value);
}
}
if(row==6 || row==7 || row==8)
{
if(col==0 || col==1 || col==2)
{
contains=checkSquare(6, 0, value);
}
if(col==3 || col==4 || col==5)
{
contains=checkSquare(6, 3, value);
}
if(col==6 || col==7 || col==8)
{
contains=checkSquare(6, 6, value);
}
}
return(contains);
}
public void Print()
{
System.out.print(" - - -\n");
for(int row=0; row<9; row++)
{
System.out.print("| ");
for(int col=0; col<9; col++)
{
System.out.print(+ Table[row][col] + " ");
if(col==2||col==5||col==8)System.out.print("| ");
}
System.out.print("\n");
if(row==2||row==5||row==8)System.out.print(" - - -\n");
}
System.out.print("\n");
}
boolean checkSquare(int row, int col, int value)
{
boolean contains=false;
int r0w=row, c0l=col;
for(int n=0; n<9; n++)
{
if(Sudoku.Final.Table[r0w][c0l]==value)
{
contains = true;
break;
}
if(++c0l > col+2)
{
c0l=col;
r0w++;
}
}
return(contains);
}
}
null
As mentioned above, with the little info provided we can only make guesses. Is it possible that you different versions of your class in different locations and that you are mistakenly trying to run older incorrect versions.
It is extremely strange. It seems to work some times but sometimes it doesn't.
I havent a clue, but it seems to break if i don't give it enough time to think before i press the option button again.
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Sudoku extends JFrame
implements ActionListener
{
static Numbers One= new Numbers(1);
static Numbers Two= new Numbers(2);
static Numbers Three = new Numbers(3);
static Numbers Four= new Numbers(4);
static Numbers Five= new Numbers(5);
static Numbers Six= new Numbers(6);
static Numbers Seven = new Numbers(7);
static Numbers Eight = new Numbers(8);
static Numbers Nine= new Numbers(9);
static Strings Possibilities = new Strings(); //Must be below Numbers declarations
static Solution Final= new Solution();
static Solution Initial= new Solution();
JButton btnOption = new JButton("Generate");
JButton[][]btn = new JButton[9][9];
public Sudoku()
{
super("Sudoku");
setSize(250, 290);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
setVisible(true);
FlowLayout flow = new FlowLayout();
pane.setLayout(flow);
flow.setHgap(0); flow.setVgap(0);
for(int r=0;r<9; r++)
{
for(int c=0;c<9; c++)
{
btn[r][c] = new JButton();
btn[r][c].setMargin(new Insets(0,0,0,0));
btn[r][c].setPreferredSize(new Dimension(25,25));
btn[r][c].setText("0");
btn[r][c].setFocusPainted(false);
btn[r][c].addActionListener(this);
btn[r][c].setActionCommand(Integer.toString(r)+Integer.toString(c));
pane.add(btn[r][c]);
}
}
btnOption.addActionListener(this);
btnOption.setFocusPainted(false);
pane.add(btnOption);
setContentPane(pane);
}
public static void main(String[] args)
{
Sudoku mainFrame = new Sudoku();
}
public void actionPerformed(ActionEvent e)
{
JButton buttonPressed = (JButton)e.getSource(); //Identify button pressed
int btnRow=-1, btnCol=-1;
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
if(buttonPressed.equals(btn[row][col]))
{
btnRow = row; btnCol=col;
}
}
}
if(btnRow!=-1 && btnCol!=-1)
{
String buttonText = btn[btnRow][btnCol].getText();
int buttonInt = Integer.parseInt(buttonText);
if(buttonInt <9) buttonInt += 1;
else buttonInt = 0;
buttonText = Integer.toString(buttonInt);
btn[btnRow][btnCol].setText(buttonText);
}
else if ("Generate".equals(e.getActionCommand()))
{
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
Initial.Table[row][col]=Integer.parseInt(btn[row][col].getText());
}
}
Final.setTable();
Final.Generate();
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
btn[row][col].setText(Integer.toString(Final.Table[row][col]));
}
}
btnOption.setText("Reset");
btnOption.setActionCommand("Reset");
btnOption.setToolTipText("Click this button to reset");
}
else if("Reset".equals(e.getActionCommand()))
{
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
btn[row][col].setText("0");
}
}
btnOption.setText("Generate");
btnOption.setActionCommand("Generate");
btnOption.setToolTipText("Click this button to generate a solution");
Initial.ZeroTable();
restart();
}
}
static void Update()
{
One.Update(1);
Two.Update(2);
Three.Update(3);
Four.Update(4);
Five.Update(5);
Six.Update(6);
Seven.Update(7);
Eight.Update(8);
Nine.Update(9);
Possibilities.Update();
}
static void restart()
{
Sudoku.One.Initialise(1);
Sudoku.Two.Initialise(2);
Sudoku.Three.Initialise(3);
Sudoku.Four.Initialise(4);
Sudoku.Five.Initialise(5);
Sudoku.Six.Initialise(6);
Sudoku.Seven.Initialise(7);
Sudoku.Eight.Initialise(8);
Sudoku.Nine.Initialise(9);
Sudoku.Final.setTable();
}
}
class Numbers
{
int[][]Table = new int[9][9];
public Numbers(int value)
{
Initialise(value);
}
void Initialise(int value)
{
for(int row=0; row<9; row++)//Nested for loops used
{//to address each cell
for(int col=0; col<9; col++)//of the objects table
{
Table[row][col]=value;
}
}
}
void Update(int value)//Update the strings table to show which values are still available for the puzzle
{
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
boolean rowcon;
boolean colcon;
if(Sudoku.Final.Table[row][col]>0)Table[row][col] = 0;
rowcon = Sudoku.Final.getRow(row, value);//Check the current row for the table value
if(rowcon==true) Table[row][col] = 0;
colcon = Sudoku.Final.getCol(col, value);//Check the current col for the table value
if(colcon==true) Table[row][col] = 0;
boolean square = Sudoku.Final.getSquare(row, col, value); //Check the square the cell is in for the table value
if(square==true) Table[row][col] = 0;
}
}
}
void Zero(int row, int col) //Redundant, but can be used to speed up processing
{
Table[row][col] = 0;
}
}
class Strings
{
static String[][]Table = new String[9][9];
String One, Two, Three, Four, Five, Six, Seven, Eight, Nine;
public Strings()
{
Update();
}
void Update()
{
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
if(Sudoku.One.Table[row][col]==1)One="1"; else One="";
if(Sudoku.Two.Table[row][col]==2)Two="2"; else Two="";
if(Sudoku.Three.Table[row][col]==3)Three="3"; else Three="";
if(Sudoku.Four.Table[row][col]==4)Four="4"; else Four="";
if(Sudoku.Five.Table[row][col]==5)Five="5"; else Five="";
if(Sudoku.Six.Table[row][col]==6)Six="6"; else Six="";
if(Sudoku.Seven.Table[row][col]==7)Seven="7"; else Seven="";
if(Sudoku.Eight.Table[row][col]==8)Eight="8"; else Eight="";
if(Sudoku.Nine.Table[row][col]==9)Nine="9"; else Nine="";
Table[row][col] = One+Two+Three+Four+Five+Six+Seven+Eight+Nine;
}
}
}
}
class Solution
{
static Random generator = new Random();
int[][]Table = new int[9][9];
public Solution()
{
ZeroTable();
}
void setTable()
{
Table=Sudoku.Initial.Table;
Sudoku.Update();
}
void ZeroTable()
{
for(int row=0;row<9;row++)
{
for(int col=0; col<9; col++)
{
Table[row][col]=0;
}
}
}
void Generate()
{
while(containsZeros()==true)
{
genCell();
Sudoku.Update();
}
}
private void genCell()
{
int row=0, col=0, cellLength=9;
for(int r=0; r<9; r++) //Following for loops used to choose the cell with the minimum possibilities
{
for(int c=0; c<9; c++)
{
if(Sudoku.Possibilities.Table[r][c].length()<cellLength)
{
if(Sudoku.Possibilities.Table[r][c].length()==0){}
else
{
cellLength = Sudoku.Possibilities.Table[r][c].length();
row = r;
col = c;
}
}
}
}
int value=0, index=0, length=Sudoku.Possibilities.Table[row][col].length();
if(length>0)
{
index=generator.nextInt(length);
value = Sudoku.Possibilities.Table[row][col].charAt(index);//This returns the ascii code of the char at point index
}
else
{
Sudoku.restart();
}
//Convert the ascii code to its char equivelent but as an integer using these else if statements
if(value=='1')value=1;
else if(value=='2')value=2;
else if(value=='3')value=3;
else if(value=='4')value=4;
else if(value=='5')value=5;
else if(value=='6')value=6;
else if(value=='7')value=7;
else if(value=='8')value=8;
else if(value=='9')value=9;
Table[row][col]=value;
}
private boolean containsZeros()
{
boolean contains=false;
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
if(Table[row][col]==0)
{
contains=true;
break;
}
}
if(contains==true) break;
}
return(contains);
}
boolean getRow(int row, int value)
{
boolean contains=false, end=false;
int col=0;
while(contains==false && end==false)
{
if(Sudoku.Final.Table[row][col]==value) contains = true;
else contains = false;
col++;
if(col>8) end=true;
}
return(contains);
}
boolean getCol(int col, int value)
{
boolean contains=false, end=false;
int row=0;
while(contains==false && end==false)
{
if(Sudoku.Final.Table[row][col]==value) contains = true;
row++;
if(row>8) end=true;
}
return(contains);
}
boolean getSquare(int row, int col, int value)
{
boolean contains=false;
if(row==0 || row==1 || row==2)
{
if(col==0 || col==1 || col==2)
{
contains=checkSquare(0, 0, value);
}
if(col==3 || col==4 || col==5)
{
contains=checkSquare(0, 3, value);
}
if(col==6 || col==7 || col==8)
{
contains=checkSquare(0, 6, value);
}
}
if(row==3 || row==4 || row==5)
{
if(col==0 || col==1 || col==2)
{
contains=checkSquare(3, 0, value);
}
if(col==3 || col==4 || col==5)
{
contains=checkSquare(3, 3, value);
}
if(col==6 || col==7 || col==8)
{
contains=checkSquare(3, 6, value);
}
}
if(row==6 || row==7 || row==8)
{
if(col==0 || col==1 || col==2)
{
contains=checkSquare(6, 0, value);
}
if(col==3 || col==4 || col==5)
{
contains=checkSquare(6, 3, value);
}
if(col==6 || col==7 || col==8)
{
contains=checkSquare(6, 6, value);
}
}
return(contains);
}
public void Print()
{
System.out.print(" - - -\n");
for(int row=0; row<9; row++)
{
System.out.print("| ");
for(int col=0; col<9; col++)
{
System.out.print(+ Table[row][col] + " ");
if(col==2||col==5||col==8)System.out.print("| ");
}
System.out.print("\n");
if(row==2||row==5||row==8)System.out.print(" - - -\n");
}
System.out.print("\n");
}
boolean checkSquare(int row, int col, int value)
{
boolean contains=false;
int r0w=row, c0l=col;
for(int n=0; n<9; n++)
{
if(Sudoku.Final.Table[r0w][c0l]==value)
{
contains = true;
break;
}
if(++c0l > col+2)
{
c0l=col;
r0w++;
}
}
return(contains);
}
}
Wild stab in the dark, you have an infinite loop somewhere. Trying adding print statements in your loops.
Is that possible?
I am using swing
System.out.print(""); seems to do absolutely nothing
How do I output to the console (cmd.exe) whilst using swing?
This is something I have often wondered how to do.
It will make understanding where the problem is occuring so much easier.
flounder is right (of course). I guess the delta house was right in taking in a legacy.
One infinite loop is in your Solution class, here:
void Generate()
{
while (containsZeros() == true)
{
genCell();
Sudoku.Update();
}
}
Do you know what. I am completely baffled now
I have been looking at it, I wrote that bit so long ago I can't remember some bits.
Anyway. I do not understand if this is an infinate loop, how I have managed to get a solution to be output. I know I have a checkZeros method somewhere that checks to see if there are any zeros in the solution.
It has to be breaking somewhere else I wouldn't be getting a solution.
Message was edited by:
emdiesse
Ahah.. Yes. I was being blind. There is the while loop that uses containsZeros()
Hi pete. You are indeed right. I was not aware of this but when the program freezes the reason is an infinate loop in the generate method of the solution class.
I am confused why sometimes it works and cometimes it doesn't (because sometimes i get a solution printed to the screen, other times i do not.
Do you know why this is happening?
> Do you know why this is happening?
No. 1) I haven't had a chance to look over all of your code. 2) I'm not that smart at java just yet. I found that infinite loop by brute force -- put a bunch of System.out.println's throughout the code and see where one gets printed over and over and over.
Perhaps someone else will be able to help you. All I ask is this: If you do find a solution, please post it back here for my (and everyone else's) education.
Much luck
/Pete
By the way, Sudoku code presents some very cool problems. I've been trying to build my own after seeing your code. all I've got so far though is user interface, not the actual game just yet.
Good luck mate :DIt's been fun
Been going through your code one more time, and w/out more comments find it hard to follow. If you see this post and you still want someone to review, please post comments. You don't have to repost the code, just the class and large method headings w/ descriptions of what they are supposed to do.
/Pete