• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help I am stuck !!

Updated Code !

To make things simple

- I am constructing a Sudoku puzzle.
- After thinking about it , I made 9 arrays (containg 9 elements each) representing 9 rows.
- the columns of the Sudoku puzzle are represented by the order of the number in an array.
- The reason for the use of rows and coulmns is because I need to read the values in from a text file ( SudokuNmbr.txt) . each line contains something like this 0 1 1 ( these number indicate the column 0 , row 1 has a number 1 in it)
I don't have the option of how to format the text file so this is how I am working with things.
- the format of the info in the text file is as follows in my attahced code.
and the Sudoku 9x9 grid will look as follows for this example pic

What is missing for me :

1- The way I wrote the loop in the populateRows method it only reads the 1st line in the text file and fills in the 3 values only, I want something that instead goes through the whole text file and stops once it reaches the -1 -1 -1 at the end. So that way I can fill the entire Sudoku grid in one loop 😉

2- In my editSudoku method I am stuck at the end, what I want to do is :

- clmn (in the editSudoku method) can't be used in the Long (because it;s going to be used to denote the array element number) so I have to convert it into an Int how do I do that ?

Pretty much after I fix those two issues, all I will have to do is add a method that will look throught the textfile for a match to what the user is trying to edit and if it returns false I will go ahead and let the user change the value.

This is due very soon and your help will not be forgotten.
 
it's been a while for me, but

BufferedReader rdr = new BufferedReader (readFile);
readNumbers = new StringTokenizer(rdr.readLine());
while (readNumbers.hasMoreTokens()) {

seems wrong. the loop you do just does that over and over until there are no more tokens (of which there should be 3 initially). the while loop should be "one level" up.

i forgot the method, but it should be something like

while( readNumbers = new StringTokenizer(rdr.readLine()) ){
or
while (( line = input.readLine()) != null){
//then you manipulate line with tokenizer
}

(from http://www.javapractices.com/Topic42.cjp )
 
Originally posted by: rainypickles
it's been a while for me, but

BufferedReader rdr = new BufferedReader (readFile);
readNumbers = new StringTokenizer(rdr.readLine());
while (readNumbers.hasMoreTokens()) {

seems wrong. the loop you do just does that over and over until there are no more tokens (of which there should be 3 initially). the while loop should be "one level" up.

i forgot the method, but it should be something like

while( readNumbers = new StringTokenizer(rdr.readLine()) ){
or
while (( line = input.readLine()) != null){
//then you manipulate line with tokenizer
}

(from http://www.javapractices.com/Topic42.cjp )


I tried what you said, I tried both codes to be exact, and the result is still not changed , I still get

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at Sudoku1.Sudoku1() (Unknown Source)
at Sudoku1.main(java.lang.String[]) (Unknown Source)
at gnu.java.lang.MainThread.call_main() (/usr/lib/libgcj.so.6.0.0)
at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)

also "readNumbers = new StringTokenizer(rdr.readLine());" should be
readNumbers == new StringTokenizer(rdr.readLine()); , because the above is will return a boolean, but I am sure you meant the second 😉
 
1) You declare a number of arrays to represent each row. You might want to change it to one large matrix (2 dimensional array) int[NUM_ROWS][NUM_COLUMNS]. That way you can index it by board[row][column]
2) Looks like when you declare those arrays, you only declare the column size as '8'. i.e. you give Row0-Row8 (equaling eight rows), but declare each to only be of size 8 (not nine)
3) Change to use strictly ints instead of long values, and you won't need to worry about converting between the types.
4) rainypickles' second method to input should work (and will, once your row size/column declarations are correct). I'm not sure about the first, since you're trying to form a tokenizer on a null value (when there's no more lines to read in).
 
Originally posted by: diegoalcatraz
1) You declare a number of arrays to represent each row. You might want to change it to one large matrix (2 dimensional array) int[NUM_ROWS][NUM_COLUMNS]. That way you can index it by board[row][column]
2) Looks like when you declare those arrays, you only declare the column size as '8'. i.e. you give Row0-Row8 (equaling eight rows), but declare each to only be of size 8 (not nine)
3) Change to use strictly ints instead of long values, and you won't need to worry about converting between the types.
4) rainypickles' second method to input should work (and will, once your row size/column declarations are correct). I'm not sure about the first, since you're trying to form a tokenizer on a null value (when there's no more lines to read in).


Man I must have been really tired not to notice that the arrays were of size 8 😱 while they should have been 9 🙁
And a 2 dimensional array is very reasonable and will fit perfectly in there, but alot of code has to be rearranged so I'll see if it's worth it or not after I have typed everything in.
Concerning using ints restrictly, well I would have done that a long time ago, but my instructor supplied me with this SImpleInput.java along time ago , and I got stuck on the habit of including that class in my projects, and the reason why I had to use Longs instead of ints is becasue it doesn't have an inputInt mehtod, but I also thought it would come handy if I would be able to know how to convert between ints and longs , becasue I couldn't see anything that can do that yet!

4- rainypickles ? I am not sure what you meant to say at point 4, but I 'll restate what I want to do with the tokenizers, I have the text formatted as :

011
022
025
134...etc
So all I want to do is for it to go on and let me read the entire Sudoku values in the text file SudokuNmbrs.txt untill it reaches the end of the text file , at that point I want it to stop reading and I would have all my values read and stored in the arrays that I need them to be in. What is going on ATM is that the first line is being read and that's about it.

I also have another problem, lets say I read in all the values to the arrays sucesfully , what about the ones that weren't touched yet, when I print the grid will they print out or will they give me an error ?

Thank you for your patience :thumbsup:
 
-To convert, you can use : new Long( your_value_here).intValue() to convert a long to an int.
-What I meant by #4 is that the method suggested by rainypickles should work, once your array bounds are correct. The problem was you were accessing at index 8, etc. Once you have that fixed, give their suggestion another go.
-I belive that primitives are all assigned default values (in your long case, 0), so you should be safe printing out your uninitialized entries. If it does give you an error, you will have to initialize all values beforehand.

Edit: Added some pseudocode to illustrate 4
 
Originally posted by: diegoalcatraz
-To convert, you can use : new Long( your_value_here).intValue() to convert a long to an int.
-What I meant by #4 is that the method suggested by rainypickles should work, once your array bounds are correct. The problem was you were accessing at index 8, etc. Once you have that fixed, give their suggestion another go.
-I belive that primitives are all assigned default values (in your long case, 0), so you should be safe printing out your uninitialized entries. If it does give you an error, you will have to initialize all values beforehand.

Edit: Added some pseudocode to illustrate 4


I have worked according to your advice and changed my arrays int one 2D array and wow everything is much easier now thanx !!
and concerning converting in between ints and longs, I need to do it the other way around so I can put the values in the arrays, how do I change a Long into an int (since the values of the longs aren't even larger than 9 I shouldn't be having any issues with such a conversion ) I tried : new Integer(clmn).LongValue(); for example and it didn't work ?
Once I settle this conversion I will go over the rest of the stuff 😉
 
I may be missing something obvious, but why are you using Longs? What's wrong with integers for sudoku?
 
You're asking to change a long into an int, right? ex)

long value;
// Assign the value from whatever your source
int intValue = (new Long(value)).intValue();
 
well, diegoalcatraz helped you out already.

as for
"also "readNumbers = new StringTokenizer(rdr.readLine());" should be
readNumbers == new StringTokenizer(rdr.readLine()); , because the above is will return a boolean, but I am sure you meant the second 😉"

i really did mean a single =. i havent coded in a while, but in some language (java, perl, something) if there were no lines to be read, it would be null, readNumbers would be null, and thus the statement would be false. then the loop would end.

it is really sloppy, but for some reason i thought of it. do it diegoalcatraz's way. =D
 
Originally posted by: Atheus
I may be missing something obvious, but why are you using Longs? What's wrong with integers for sudoku?

Well as I said before that I got into the habit of using this class that my instructor supplied with for another project a long time ago and I have been since then using it in my projects for user input, and it doesn't have a method for accepting int input , also I thought it would come handy to know how to convert in between ints and Longs, and especially in this case the numbers aren't even ore than 9 so it wouln't cause any innaccuracy to convert from Longs to ints that's all 😉
 
Originally posted by: rainypickles
well, diegoalcatraz helped you out already.

as for
"also "readNumbers = new StringTokenizer(rdr.readLine());" should be
readNumbers == new StringTokenizer(rdr.readLine()); , because the above is will return a boolean, but I am sure you meant the second 😉"

i really did mean a single =. i havent coded in a while, but in some language (java, perl, something) if there were no lines to be read, it would be null, readNumbers would be null, and thus the statement would be false. then the loop would end.

it is really sloppy, but for some reason i thought of it. do it diegoalcatraz's way. =D

Thanx I understand what you meant 😀
Originally posted by: diegoalcatraz
You're asking to change a long into an int, right? ex)

long value;
// Assign the value from whatever your source
int intValue = (new Long(value)).intValue();


Great now I have converted the values into ints.

Now to the last tricky part, once the user decides to change a certain value, I need to see if he is allowed to change that value or not.
Now the best waty to do this is to see if that value exists in the file SudokuNmbrs if yes then I will tell the user that he can't change it, and if the value isn't in there in the file I will let the user go ahead and change it.

So lets say that the user plans to change the value of the square in the 1st row and lies in the 4th column meaning RowCol[0][3], how do I go about checking if that value is in the file or not ,so in another sense I need to go into the file and look for 0 3 x , and see if there is anything that starts with 0 3 as the first two tokens ( the value represented by x doesn't matter ).
 
Did I right my populateRows right , I am getting the following everytime I use it.

xception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken() (/usr/lib/libgcj.so.6.0.0)
at Sudoku1.populateRows() (Unknown Source)
at Sudoku1.printhelp() (Unknown Source)
at Sudoku1.main(java.lang.String[]) (Unknown Source)
at gnu.java.lang.MainThread.call_main() (/usr/lib/libgcj.so.6.0.0)
at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)
 
You call readline() too many times on the Buffered Reader, and you're never refreshing the tokenizer (supplying more lines to parse). Try this instead:
 
Originally posted by: diegoalcatraz
You call readline() too many times on the Buffered Reader, and you're never refreshing the tokenizer (supplying more lines to parse). Try this instead:

Great it's now doing exactly what is requird nice!!

Now here is something I have never heared off, in the process of checking that a row or column contains only numbers from 1 to 9 strictly how do I go through my array and see if a certain value existsor not, also how do I look for null values , you know in case the user left a blank square I am supposed to remind him / her that he / she left a blank square.

I am not sure how the syntax for that would be ?

 
Are you referring to a for loop? (See attached code)

As for null values, I think there's something I overlooked. I can't remember if Java is like this (I might be thinking of C 2d array init), but you might have to do more initialization of your matrix:

public Sudoku1 () {
for ( int i =0; i < 9; i++ ) {
RowCol[ i ] = new int[9];
}
... // Other constructor details
}
 
Originally posted by: diegoalcatraz
Are you referring to a for loop? (See attached code)

As for null values, I think there's something I overlooked. I can't remember if Java is like this (I might be thinking of C 2d array init), but you might have to do more initialization of your matrix:

public Sudoku1 () {
for ( int i =0; i < 9; i++ ) {
RowCol[ i ] = new int[9];
}
... // Other constructor details
}


I don't think I understood you right! To be sure let me explain this again, after the user goes in and inputs done in the commandline, I will have a method that goes into my 2d array and check that this array has non-repating values from 1 to 9 in each row and in each column possible in my matrix.
I don't think it has anything to do with the way I declare my 2D array.

I am considering doing it in a loop that will have a boolean with inital value of false, and at each step of the loop an integer will be tested for, so at the begenning it goes and looks for 1 and if it finds it it keeps the boolean at false and keeps going if the boolean stays false then it will make the integer the search is being done for as 2 and keeps going through my matrix looking for a row or column that doesn't have 2 in it and so on for the rest of the numbers. Once it's done with the numbers it will finally look for null in my matrix if nothing makes the boolean true then at the end I will infrom the user that he / she has everything right in there, if not I will have to indicate what's the issue and where the error was detected.
 
What I mentioned above, I now believe has to do with another language, so you can ignore it.

From what I can understand, you need to conduct checks when the user inputs a value. You need to make sure that they're not inputting an int that already exists within that column or row. Is that correct? Or do you mean this check to run on the entire board, checking every row and column?

First, looks like you'll need to either scan the entire file each time the person enters a new value (to make sure they're not overwriting a starting value). The alternative to this is to have additional storage, say another matrix of booleans to indicate if each cell can be changed. Or you could redisgn your game board to have each cell contain both an int value and said flag.

I'm not too familar with Sodoku, but I also think you need to check the 3x3 grids as well as rows/columns to avoid duplicates.
 
Your logic described above seems sound.

You will have to search the complete array, unless you setup a method for have a sorted array to check on.
 
Originally posted by: diegoalcatraz
What I mentioned above, I now believe has to do with another language, so you can ignore it.

From what I can understand, you need to conduct checks when the user inputs a value. You need to make sure that they're not inputting an int that already exists within that column or row. Is that correct? Or do you mean this check to run on the entire board, checking every row and column?

First, looks like you'll need to either scan the entire file each time the person enters a new value (to make sure they're not overwriting a starting value). The alternative to this is to have additional storage, say another matrix of booleans to indicate if each cell can be changed. Or you could redisgn your game board to have each cell contain both an int value and said flag.

I'm not too familar with Sodoku, but I also think you need to check the 3x3 grids as well as rows/columns to avoid duplicates.


As the assignment goes, at the end oncethe user thinks he is done, I am required to notify the user if he / she did it correctly or not, I am not obliged to do any checks when the user inputs values in, I only have to make sure he did things right at the end.
So when a user is done, he types done , then that will launch a method called checkSudoku , and checkSUdoku will do the following :

1- look for null / or zero entries, because both of those are not allowed . So if any of such entries are found inside my 2D Array (RowCol[][]) a message is printed notifying the user that he has a null / zero entry that he might want to check.

2- If no null / zero entries were discovered, then we need to look inside each clumn and Row in the matrix , example :

Row 1 would be RowCol[x][y] where x is 0 and the y value needs to go in between 0 & 8, as you increment y you are moving along the first Row.
I need to setup a loop or something of that sort ( perhaps more than one loops inside each other) inorder to see that I have one and only one match of all numbers from 1-9 .

3-concerning when a user inputs a value, the way I accept or decline his input is if the location of the value he is trying to change has no atch in my file, so how do I go about that ?? I need a line of code that goes through a line and compares if the intRow and intCol that I have obtained from the user input exist or not in the file that is formatted exactly as I am describing it below :

0 1 1
0 2 3
0 1 5
and the file keeps going in the 1st line for example the first entry menas row 0 and the second entry means column 1 and the last entry means the value of the number to be placed in row0,column 1.
please check my updated code for the project, and take a look, perhaps give it a run in an IDE.

here is the SimpleInput class so the program runs .

 
I am still stuck on determining how to get this working(finding out if a certain row or column has only non-repeated digits from 1 to 9 or not), I have tried lots of different techniques , but this is like nothing I have done before wold appreciate some pointers guys 🙂
 
Back
Top