• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading a file and validating its contents  RSS feed

 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have this method that reads in a sudoku file while checking if these properties are true throughout the process.

1.) They have to be numbers(no floating points or letters). The only exception is if it is a space. In which case you skip it. Note the file may have more than one space.
2.) Each number has to be from 1 to N where N is the highest number you can place in a cell. For example in a 3 by 3 the highest is 9.

This is just one other concern. The user may enter the number of columns and rows the board has but it may not match the board in the file. This means that numbers that should have been read were not read because the board in the file was too big. The other case is if the board is too small

The method is following the convention where caller is the one handles the exception not the calle. So I'm throwing a FileNotFoundException and a IOException.

So far I've been trying to read file and the problem I've been having so far is that if the file has more than one space when I use the split function, it includes in the string array and when I try to parse it, I get an error. How do I deal with this problem.


 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.split takes a regular expression so I assume you could pass something like "[\\s]+".

Alternatively you could pre-process the next line first and remove all duplicate whitespaces.

One way to do that is to go through all its characters (String.toCharArray()) in a loop and copy the content into a new string. Keep the last parsed character in a reference and if it's a space you can update it with the current field and don't copy the current field to your target string, otherwise if the last char was not a white space you copy the current char to your target string. Of course the solution would be more elegant if you build your copy algorithm as a Java8 lambda expression. ;-)

If possible I'd stick to the first choice though, less work, less error potential and probably also more efficient.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lewis manuel wrote:. . . They have to be numbers(no floating points or letters). The only exception is if it is a space. In which case you skip it. . . .
And what do spaces mean? Do they separate successive numbers? How does your file represent empty cells? Are they shown by spaces, 0s or what?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Pflugradt wrote:String.split takes a regular expression so I assume you could pass something like "[\\s]+". . . .
I think "\\s+" without the square brackets will do, too. But you cannot parse that file until you know how empty cells are shown. The top row of the Sudoku in today's newspaper reads:-
| | | | |1| |7| | |
How would that be represented in your file? I can think of several representations:-If you use spaces to represent empty cells, then you can use " " or "\\s{1}" as a regex for splitting. At least I think you can. Let's try it:-

I did and it didn't work. You try itSee. The middle space and the trailing spaces disappear. Back to the drawing board. In that format of String you would have to count the individual spaces to put the numbers in the right places.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of a 4 * 4

0 0 0 0
0 4 0 0

0 0 2 0
0 0 0 0

Notice how some of them have extra spaces so that it is more clear that they are subgrids. A 0 here you can imagine represents empty space or a cell in which a number has not been inserted. Regardless, all these numbers are to be read.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My friends, does java have something like scanf? If so, I can do conversion specifiers
to note that there is an integer and a space. I did this in c and it worked.

Edit: Nope, guess there isn't.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a solution using scanner

String line = read.nextLine();
String [] numbers = line.trim().split("\\s+");

The only time it fails is when it reaches a break like this.

0 0 0 0
0 0 0 0
<------- Fails here. need to skip this.
0 0 0 0

if(line.isEmpty()) // works
{
read again.
}
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how my method looks right



My next two goals are to detect if cellNumber is not a number and if the dimensions entered by the user does not match what is in the file. Here is what I mean by that last statement.

suppose the user says that the file is 9 by 9 board but the board is 4 by 4. This means we are only going to read 16 cells as opposed to 81 cells. The opposite could happen.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you say the numbers run from 1 to n and then show us 0s?
What was the solution in your other thread?
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lewis manuel wrote:I found a solution using scanner

Please show us
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay it is clear that I'm going to have explain this again.

A 0 just means empty space. I could have used a dot for it but I when with a zero(they both have the same logic).

N is the biggest integer you can have in any cell.

in a 4 by 4, any cell can have a number from 1 to 4 but can't be greater than that.

here is an example of that logic.



The solution that I found with scanner is two posts above your post Liutauras Vilda.

it worked I guess because by default scanners skips whitespaces.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lewis manuel wrote:

Anyway, don't use File along with Scanner. File is considered as legacy for I/O since Java 7. That could lead you to all sort of problems.

Change that to:
or

By the way, why you named it as a fileName? It is not file name in there is it? There should be a full path to the file. If you'd supply file name only, you'd read file from current directory, while the actual file could be somewhere else.

Note: Path and Paths you'll need to import.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have the txt file in the same location as my project but what if it was in another location(not in the relative directory and so I would have to specify the path of its location)? Would that be problem with the I have it setup? I should note that the file is inside the main project folder and not in the src folder or anything like that.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lewis manuel wrote:Well I have the txt file in the same location as my project but what if it was in another location(not in the relative directory and so I would have to specify the path of its location)? Would that be problem with the I have it setup?
That should be fine. In case what's in the parentheses written, yes, you'd need to supply full path.

If you say you have your file in /etc/rc.conf

You'd write

Also you can have a look to this short tutorial about Path. Also have a look why File is no longer recommended to use here.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool I will definitely take a look at that. Now onto the next step. I mention before of either the method throwing another exception like InputMismatchException in case it is not an integer but I would like to know if there is a clever to detect this and return false because we expected the board to be numerical entirely. Just to be clear, I do not want to skip anything just return false if I find one case in which the cell is not an integer.
 
lewis manuel
Ranch Hand
Posts: 64
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I went with creating a simple boolean method that checks if it is numeric. Here it is.



Whatever extreme cases there are, it's suffice for what I'm trying to do.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two other ways to do this. Probably the simplest is

with a try/catch around it. Or you could use regular expressions:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lewis manuel wrote:So I went with creating a simple boolean method that checks if it is numeric. Here it is.

lewis, may I suggest something? StopCoding.

I've been going down this thread, and all I see is you telling us how you've already implemented something that you're asking questions about.
Furthermore, your logic seems to be driven by the input, rather than the other way around.

Normally, a designer will look to find the best way to input data into an application - and oddly enough, it often coincides with how that same data is output.

So, first question: How do you output your Sudoku tables?

Next question: Why all these space lines? They only confuse things for me (but I never was any good at Sudoku ).

Either tell us how you would like to input the data and why; or tell us what you want to input, and ask for advice. It gets very difficult if you do both at the same time.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!