Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with a lab!

  1. #1
    Junior Member Spicy McHaggis's Avatar
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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]+" ");
    }
    }
    }
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default 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.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting