Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:34:54+00:00 2026-06-02T22:34:54+00:00

This is supposed to be a Sudoku Puzzle solver and it is required I

  • 0

This is supposed to be a Sudoku Puzzle solver and it is required I use a two dimensional ArrayList for the puzzle.

I’m trying to fill an ArrayList using numbers from a txt file. The code in my test class can make a 9×9 ArrayList and fill it with numbers 1-9 using the loop I made to test how filling a two dimensional ArrayList works.

import java.util.*;
import java.io.*;

public class test
{
    public static void main(String args[])
    {
        ArrayList<ArrayList<Integer>> data = new ArrayList<ArrayList<Integer>>(); 

    //Loop to add 9 rows
    for(int i=1; i<=9; i++)
    {
        data.add(new ArrayList<Integer>());
    }

    //Loop to fill the 9 rows with 1-9
    for(int k=0; k<9; k++)
    {
        for(int j=1; j<=9; j++)
        {
            data.get(k).add(j);
        }
    }

    //Loop to print out the 9 rows
    for(int r=0; r<9; r++)
    {
        System.out.println("Row "+(r+1)+data.get(r));
    }

    //Reads the file. Need to use this to set the array
    File file = new File("F:\\Data Structures 244\\FINAL PROJECT\\SudokuSolver\\sudoku.txt");

    try {

        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            System.out.println(line);
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    }
}

My problem comes when I try to take the numbers I have stored in the txt file and use them to fill the ArrayList in the order they are read. I tried this code so that it would read the number from the txt file and place it into the ArrayList by having it repeat so it would do the first 9 numbers from the txt file into the first row of the ArrayList and then go to the next row in the txt file as well as the ArrayList to fill those numbers.

File file = new File("F:/Data Structures 244/FINAL PROJECT/SudokuSolver/sudoku.txt");

     //Needs this try and catch
     try {
        Scanner solutionFile = new Scanner(file);
            int cell=0;
        while (solutionFile.hasNextInt()) 
            {
                //Loop to fill the 9 rows with the numbers from the sudoku.txt
                for(int k=0; k<9; k++)
                {
                    for(int j=1; j<=9; j++)
                    {
                    cell = solutionFile.nextInt();
                    data.get(k).add(cell);
                    }

                }   
        }
        solutionFile.close();
       } 

      catch (FileNotFoundException e) 
      {
        e.printStackTrace();
    }

     for(int r=0; r<9; r++)
        {
            System.out.println("Row "+(r+1)+data.get(r));
        }

I get an Exception in thread "main" java.util.NoSuchElementException
with this line cell = solutionFile.nextInt();

This is the sudoku.txt file

346791528
918524637
572836914
163257489
895143762
427689351
239415876
684372195
751968243

I tried it like that at first and get that error but then I try taking all of the numbers and putting them on one line so my for loops will only read them 9 numbers at a time but when I test to print the ArrayList or what should be in it after it adds them the entire ArrayList is blank.

What is the problem with it not reading the numbers from the file and putting them into the ArrayList so it will print?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:34:56+00:00Added an answer on June 2, 2026 at 10:34 pm

    An integer can be more than one character. You’re reading the entire line as a single number and then failing after you finish the nine reads (1 for each line).

    You can reformat your text file so that the numbers are broken up: (I believe a space will work if not a new line definitely will)

    so like

    3 4 6 7 9 1 5 2 8
    ...
    

    Or you can read the whole number and parse it yourself

    int temp = solutionFile.nextInt();
    
    for(int i = 8; i > 0; i--) {
       int cell = temp / (10 * i);
       data.get(k).add(cell);
    }
    
    //add the last cell
    int cell = temp % 10;
    data.get(k).add(cell);
    

    Or you could read the value as a string and parse it

    String line = solutionFile.next();
    
    for(int i = 0; i < line.length; i++) {
       Integer cell = Integer.parseInt(line.substring(i, i+1));
       data.get(k).add(cell);
    }
    

    This isn’t a valid way to print a list of numbers (ArrayList’s toString doesn’t print the list for you you have to do it manually)

     for(int r=0; r<9; r++)
        {
            System.out.println("Row "+(r+1)+data.get(r));
        }
    

    Should be something like

     for(int r = 0; r < data.size(); r++)
     {
        System.out.print("Row " + (r+1) + ": ");
        for(Integer num : data.get(r)) {
           System.out.print(num + " ");
        }
        System.out.println(); //to end the line
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to return json content read from MySQL server. This is supposed to
This code is supposed to read postfix problems from a file and and write
I'm trying to implement block sorting. This is from the Burrows Wheeler paper .
I just got this frame for a sudoku solver, but I don't understand the
I've been trying to follow this tutorial on using Google Map View in Android.
Is this supposed to work? When i use <xs:pattern value=(!red|green|blue)/> everything is fine, but
This is supposed to work yet just says no stocks table - supposed lost
This code is supposed to be able to sort the items in self.array based
This program is supposed to determine how many units are stored in the value
OK, this probably is supposed to be the easiest thing in the world, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.