# Help with a lab!

• February 2nd, 2012, 01:12 AM
Spicy McHaggis
Help with a lab!
Hello, I was wondering if anyone could look at my code and possibly tell me where I am going wrong. It seems this has confused nearly everyone in my class and we cannot seem to figure this out. I have posted the objective that has to be accomplished in the attachments. Also this is what my code looks like (I'm sorry if there is a special way to link it, brand new to this :/ ) But basically what happens is every time I run this, I get this result:

Result:
Basically I need the "0's" to be replaced by numbers that are not zero but less than 10 (1-9) and a number cannot be repeated next to each other unless that number comes from the original sudoku puzzle. So I cannot have like 1 1 4 because the 1 did replace the 0's but it was repeated next to each other.
1 3 4 2 5 7 3 9 0
0 2 0 5 3 0 0 4 0
0 0 3 0 0 4 0 0 5
6 0 0 4 0 0 5 0 0
0 7 0 1 5 0 0 6 0
0 0 8 0 0 6 0 0 7
8 0 0 9 0 0 7 0 0
0 9 0 0 1 0 0 8 0
0 0 1 0 0 2 0 0 9

This is the Original: 0's need to be replaced with numbers between 1 and 9

1 0 4 2 0 0 3 0 0
0 2 0 5 3 0 0 4 0
0 0 3 0 0 4 0 0 5

6 0 0 4 0 0 5 0 0
0 7 0 1 5 0 0 6 0
0 0 8 0 0 6 0 0 7

8 0 0 9 0 0 7 0 0
0 9 0 0 1 0 0 8 0
0 0 1 0 0 2 0 0 9

import java.util.Scanner;
import java.io.*;
public class sudoku
{
private int x;
public sudoku()
{
x=0;
}
public static void main(String [] args) throws IOException {

int i=0;
File f = new File("single");
Scanner input = new Scanner(f);
int[]digit = new int[81];

while (input.hasNext())
{
digit[i]= input.nextInt();
i++;
}

for(int x=0;x<i;x++){
if(digit [x] == 0)
{
for(int y=1;y<10;y+=2)
{

int z;
for(z=0;z<x;z++)
{

if(y== digit [z])
break;
}
if(x==z){
digit[x]=y;
break;
}
}
}

if(x%9==0)
System.out.println("");
System.out.print(digit[x]+" ");
}
}
}
• February 2nd, 2012, 07:03 AM
Norm
Re: Help with a lab!
Looks like you have a logic error if your code doesn't work the way you want it to.
First you need to get a design for how the code is supposed to work. Perhaps as pseudo code that describes in great detail how the code show work.
Then write the code from those specifications.
If you can not see what the problem is with the way the code executes, you need to debug it.
One way to debug this kind of code is to use printlns to show the values of all the variables as they are used by the program so you can see the execution flow and the decisions that the code is making. The printout should show yuo where the code is not following your design logic.