Reputation: 13
I want insert sudoku answer with a txt file . My book instruct me to do as follows :
"It is cumbersome to enter 81 numbers from the console. When you test the program, you may store the input in a file, say CheckSudokuSolution.txt
(see www.cs.armstrong.edu/liang/data/CheckSudokuSolution.txt), and run the program using the following command:
java CheckSudokuSolution < CheckSudokuSolution.txt"
I saved CheckSudokuSolution.txt
file in same directory as my java file and tried several times. But command promp shows following error :
"Error : The system can not find the file specified".
import java.util.Scanner;
public class CheckSudokuSolution {
public static void main(String[] args) {
// Read a Sudoku solution
int[][] grid = readASolution();
System.out.println(isValid(grid) ? "Valid solution" :
"Invalid solution");
}
/** Read a Sudoku solution from the console */
public static int[][] readASolution() {
// Create a Scanner
Scanner input = new Scanner(System.in);
System.out.println("Enter a Sudoku puzzle solution:");
int[][] grid = new int[9][9];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
grid[i][j] = input.nextInt();
return grid;
}
/** Check whether a solution is valid */
public static boolean isValid(int[][] grid) {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] < 1 ||
grid[i][j] > 9 ||
!isValid(i, j, grid)
)
return false;
return true; // The solution is valid
}
/** Check whether grid[i][j] is valid in the grid */
public static boolean isValid(int i, int j, int[][] grid) {
// Check whether grid[i][j] is unique in i's row
for (int column = 0; column < 9; column++)
if (column != j && grid[i][column] == grid[i][j])
return false;
// Check whether grid[i][j] is unique in j's column
for (int row = 0; row < 9; row++)
if (row != i && grid[row][j] == grid[i][j])
return false;
// Check whether grid[i][j] is unique in the 3-by-3 box
for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if (row != i && col != j && grid[row][col] == grid[i][j])
return false;
return true; // The current value at grid[i][j] is valid
}
}
how can I compile this java program with CheckSudokuSolution.txt file using command line ?
Upvotes: 0
Views: 2533
Reputation: 868
Which commands are you typing?
I would think they should look something like follows:
java CheckSudokuSolution < CheckSudokuSolution.txt
This would be done after having compiled the code first, to create the .class files. Note that this is done in two different steps:
Only the second step will then use the text input file, and we would be more correct to say that we are running our program with CheckSudokuSolution.txt file as input; rather than compiling it.
In our example, the character < is used for passing the contents of a file as standard input to a program. This is called redirection, and there's a lot more information to find online about it. The point here is that it is not java that has problem finding the file, but rather your shell, which would look in the directory you are currently in.
Therefore you should make sure that your directory contains both the .class file, as well as your .txt input data. If they are in separate folders, perhaps look into the -cp argument for the java command, which can be used to specify where your class files are.
Upvotes: 1
Reputation: 237
Based on your code, I understand that you take the input from console and put it in grid[][]
. However, I guess you want the file name as an input and read it from file.
try{
BufferedReader reader = new BufferedReader(new FileReader(new File("<absolute path of CheckSudokuSolution.txt>")));
} catch(FileNotFoundException fe) {
fe.printStackTrace();
} finally {
}
Upvotes: 0
Reputation: 339
The file needs to be in the same directory that you are running your Java command from.
So if your classes are in a directory classes
, you should run the following command:
java -cp ./classes CheckSudokuSolution < CheckSudokuSolution.txt
Upvotes: 0