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 7 of 7

Thread: Read a text file and generate a valid Sudoku

  1. #1
    Junior Member
    Join Date
    May 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read a text file and generate a valid Sudoku

    Hi,

    I want to create a script in java that allows you to read a .text file that contains 81 numbers where each number contains 3 digits, the first digit is the row number, the second digit is the column number and the third digit is the value. I want to store these 81 values ​​in an array and check if it's a valid sudoku game.

    This is what the text file contains :

    001 012 023 034 045 056 067 078 089 102 113 124 135 146 157 168 179 181 203 214 225 236 247 258 269 271 282 304 315 326 337 348 359 361 372 383 405 416 427 438 449 451 462 473 484 506 517 528 539 541 552 563 574 585 607 618 629 631 642 653 664 675 686 708 719 721 732 743 754 765 776 787 809 811 822 833 844 855 866 877 888

    This is my attempt but it doesn't work, the first box is 1 (working) but the rest are 0 :

     
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class tp4 {
        public static void main(String[] args) {
            // Chemin vers le fichier texte contenant les 81 nombres
            String cheminFichier = "/Users/felixleblanc/Desktop/ex_cercle/src/partie1.txt";
     
            // Créez un tableau pour stocker les valeurs
            int[][] sudoku = new int[9][9];
     
            try {
                FileReader lecteur = new FileReader(cheminFichier);
                BufferedReader bufferedReader = new BufferedReader(lecteur);
     
                String ligne;
                while ((ligne = bufferedReader.readLine()) != null) {
                    // Analysez chaque ligne pour extraire les valeurs
                    int numeroLigne = Character.getNumericValue(ligne.charAt(0));
                    int numeroColonne = Character.getNumericValue(ligne.charAt(1));
                    int valeur = Character.getNumericValue(ligne.charAt(2));
     
                    // Stockez la valeur dans le tableau (soustrayez 1 car les indices commencent à 0)
                    sudoku[numeroLigne][numeroColonne] = valeur;
                }
     
                bufferedReader.close();
     
                // Affichez le tableau pour vérification
                for (int i = 0; i < 9; i++) {
                    for (int j = 0; j < 9; j++) {
                        System.out.print(sudoku[i][j] + " ");
                    }
                    System.out.println();
                }
            } catch (IOException e) {
                System.out.println("Erreur lors de la lecture du fichier.");
            }
        }
    }

    Thank you!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Read a text file and generate a valid Sudoku

    Add a print statement inside the loop that prints the value read into the ligne variable. Check that it is what you expected.
    It looks like the code expects one 3 digit number on each line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read a text file and generate a valid Sudoku

    Thank you for your response. That's not what already does the following line of code in the loop?

    System.out.print(sudoku[i][j] + " ");

    I thing there is a problem with the values that are stored in the sudoku object

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Read a text file and generate a valid Sudoku

    What was printed for the values of the ligne variable inside of the while loop? Did those values look correct?

    Sorry, I do not understand your response.

    The statement:
    System.out.print(sudoku[i][j] + " ");
    prints out what was read in the while loop and stored in the array. My doubt is that the while loop is not reading all the data correctly.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read a text file and generate a valid Sudoku

    You are right, the while loop doesn't read the data correctly. What would you do to correct this?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Read a text file and generate a valid Sudoku

    After the line is read, the split method could be used to extract each of the numbers into an array.
    An example of using split:
       String str = "d fff r grr";
       String[] strA = str.split(" ");
       System.out.println(Arrays.toString(strA));   // [d, fff, r, grr]
    Then use a loop on the array to get each of the values.

    Another way would be to use the Scanner class to read the file token by token using the hasNext and next methods.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read a text file and generate a valid Sudoku

    So, looking at your code, I think the main issue might be in how you're reading the file and converting it into a Sudoku board. Make sure you're reading the file correctly and splitting the data into a 9x9 grid. Here's a quick tip: after reading the file, split the lines and then split each line into individual numbers.

    Also, remember to check if your initial board has any conflicts before trying to solve it. Maybe add some print statements to see what's happening at each step. Debugging can be a bit like a game of spider solitaire – one step at a time!

    Keep at it, you're doing great! Just a few tweaks and you'll have that Sudoku puzzle solved in no time. Happy coding!
    Last edited by farnoshee; June 18th, 2024 at 02:51 AM.

Similar Threads

  1. What is the best way to read from a text file?
    By Kerr in forum File I/O & Other I/O Streams
    Replies: 20
    Last Post: January 4th, 2012, 05:41 PM
  2. Read text file
    By cardamis in forum Java IDEs
    Replies: 2
    Last Post: November 4th, 2011, 11:59 PM
  3. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM