• Post Reply Bookmark Topic Watch Topic
  • New Topic

read files  RSS feed

 
bunny Indhrani
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i wrote this code to read a text file but i want to read two text files at once and then wanted to compare them..
can you check this out and tell me how to do.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got some part of task correct, but that is it, full stop. Probably you should start by solving exercise logic on a piece of paper.
Define for yourself what do you mean by compare them. Number of records (lines), length of lines, same sequence of characters or any other slices of comparison.

And start by writing down the steps, i.e.:
1. Open file1
2. Open file2
3. Read file1 line.
4. Read file2 line.
5. Compare file1 line versus file2 line
6. If lines are (type of comparison), repeat steps 3 and 4
6.1 If lines are not (type of comparison), print something/do something else.

Once you're clear what you want to do, should be easier to come up with a code which solves your task.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you come up with primary steps what your program should do, then most likely you'll need to dig down to more details and rewrite all steps once again, but this time with more details.

Look at the example for instance with shop's self check out system.

Most abstract steps as I showed you in my earlier. Actually earlier I provided too much details for the beginning, start from very abstract steps, and everything on a piece of paper.Then you go down to more details. Take as an example for if line is not null.
More details. Could use as an example similar to if lines are equal or not, what to do, print that equal or not or something else.
Then you add iteration to your details, similarly as you'd read and compare multiple lines of both files, as well as you would count amount of records.

By writting down the steps you can incorporate any comparisons you need, units check if files for instance represents sales figures or inventory stock or any other comparisons needed.
But first, you need to understand what exactly do you want to do, or more precise, what do you want your program to do. In this way is also easier to see if you missed something important.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bunny lucy wrote:i wrote this code to read a text file but i want to read two text files at once and then wanted to compare them...

That's a vague - and potentially quite complex - task, involving things like Levenshtein distances; but I suspect your task is a lot simpler.

So: explain to us exactly how you want to compare. For example:
1. Can you choose one file as the "master" version?
2. Do the files always contain the same number of lines, in the same order?
3. Does each file have a predictable format?

If so, the task is probably fairly simple; if not, you may have to decide, when one file, for example, is missing a line - what that means.

Maybe if you showed us what they look like, we might be able to advise better.

Winston
 
bunny Indhrani
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,
actually my task is to compare two fields in two text files..that is,in both files i have roll_number of a student and when i compare those roll_num in both text files,then if they are same then i need to retrieve some info from the text files like student marks. so for that i need to read two files and then compare them by using matcher and pattern classes..
can you help me out with my above code..
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bunny lucy wrote:can you help me out with my above code..

It would help greatly if you could show us what these files look like, and exactly what you're (a) comparing, and (b) what you're retrieving when they're the same.

[Edit] And also what you do when they aren't the same.

Winston
 
bunny Indhrani
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay.thank you.
these are two text files,both text files have roll_num when i compare them..and if they match then i need to retrieve marks..if not match just display not found.

studentdetails.txt

Name Roll IMarks EMarks
john 101 21 45
hellen 102 20 50
allen 103 22 42
rose 104 23 55
jack 105 24 42
lucy 106 25 35
edward 107 25 42
alex 108 24 50
martin 109 23 48
rock 110 21 38

studentdetails1.txt

Roll IMarks EMarks
101 21 45
102 20 50
103 22 42
104 23 55
105 24 42
106 25 35
107 25 42
108 24 50
109 23 48
110 21 38



 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bunny lucy wrote:actually my task is to compare two fields in two text files..that is,in both files i have roll_number of a student and when i compare those roll_num in both text files,then if they are same then i need to retrieve some info from the text files like student marks.
That is why you have been asked to write down the steps in the right sequence, so we could help you better. Anyway, there are still some unclear things.
In the file examples you provided, it seems the records are in the chronological order. Can it be differently? There are only the first name in the file, can be first and middle name provided in it?

bunny lucy wrote:...compare them by using matcher and pattern classes...
Is it requirement you need to follow?
 
bunny Indhrani
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just gave example of my text file.actually one text file contains full student information like you asked me about middle name or last name including them.
for your understanding one text file is university level student information and other text file is college level student information.so in both files i will be having roll_num and i need to compare them to retrieve marks.
yes i need to use mattern and pattern class that is my task .
but firstly i was unable read two text files to compare them.
can you help me out with my above code.with that code i was able to read one file once.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For each file you want open at once, you need a separate file reader.
Liutauras gave you some pseudocode earlier. Have you managed to use that to write some code?

It's pretty much your original code, except you need two of everything within that loop.

Also, just mentioning it, but the first file has all the information in it. Why do you even need to read the second?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bunny Indhrani wrote:I just gave example of my text file.actually one text file contains full student information like you asked me about middle name or last name including them.

OK, that's much better.
for your understanding one text file is university level student information and other text file is college level student information.so in both files i will be having roll_num and i need to compare them to retrieve marks.

Right, well my first suggestion would be to name your files better: how about studentdetails.txt and universitymarks.txt?

Naming is incredibly important, both inside your program and outside, because it helps other people understand what you're doing.

yes i need to use mattern and pattern class that is my task .
but firstly i was unable read two text files to compare them.
can you help me out with my above code. with that code i was able to read one file once.

And that's all you should really need to do - unless your exercise specifically states that they want you to read the files multiple times.

File reading in Java is extremely slow and cumbersome, but Java collections (specifically, subclasses of List, Map, and Set) are spectacularly fast - and what's more, you have lots of different types to choose from.

However, in order to avoid reading your files multiple times you first have to store them somewhere. So, since you already know that your code is working for reading lines in a file, let's try that:I really haven't changed much in your program, but now I have a Java structure (a List) that contains all the lines of the file I just read, so I don't need to go back to the file to read them again.

Now suppose that you put all that code in a method:Now your main() method has something that it can re-use, viz:Woa! 2 statements (basically), and I just read both my files.

Of course the job isn't finished yet, because we still have to do the "comparison" stuff, but try that out first.
It's one of the first things you need to learn when you program:
Build up your code gradually. And TEST it at every stage.

So now you have both your files as lists of lines in your program, but you still need to break them up to get the information. Why? What are you going to do with it?
Well, your first file seems to hold information about students, so what about creating a Student class? Maybe something like:and now you can add a constructor that can create a Student from a line of text, viz:BTW: That string.split("\\s+") is a good trick to remember. It splits a String - in this case your "line" - into an array of "fields" delimited by any amount of whitespace.

And now, because you have a List of lines, you can create a List of Students, viz:You can also do something similar for your other file (a UniversityMark class?), and then, instead of comparing lines of text, you'll actually be comparing Java objects.

Now it's possible that I've gone a bit too far here, because the exercise might be to get you to use Pattern and Matcher - and BTW, it's 'Matcher', not 'mattern'. I know it was probably a typo, but you need to be very careful about these things when you program, because the compiler is NOT forgiving about sloppy typing.

But once you've got lists of lines, you can do all the matching you want, without having to read files over and over again.

So remember:
  • Write LOTS of methods, and get as much code as you can OUT of main().
  • Build up code gradually, and test it at every stage.

  • HIH

    Winston
     
    bunny Indhrani
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you.
    can you tell me about pattern and matcher classes for comparing the two text files content after reading two above text files.
     
    Carey Brown
    Saloon Keeper
    Posts: 3310
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    bunny Indhrani wrote:I just gave example of my text file.actually one text file contains full student information like you asked me about middle name or last name including them.
    for your understanding one text file is university level student information and other text file is college level student information.so in both files i will be having roll_num and i need to compare them to retrieve marks.
    yes i need to use mattern and pattern class that is my task .
    but firstly i was unable read two text files to compare them.
    can you help me out with my above code.with that code i was able to read one file once.

    In the two text files you've provided, one is a subset of the other, which means that you should only need to read one of the files. I will assume for the time being that the files are not as you've shown, otherwise you would not be insisting on reading two files. Please show snippets of ACTUAL files so that we can better understand your requirements.

    Regarding Pattern and Matcher, I don't quite see how they fit into your requirements. Pattern/Matcher can be used to return a boolean as to whether the pattern was a partial match (find) or a complete match (matches). It can also be used to parse out fields (groups) contained in a String. Comparing, for instance, roll in file-1 to roll in file-2 should only require equals() (if it's a String) or '==' (if it's numeric), there is no Pattern involved. If on the other hand, splitting out the fields using String.split() is insufficient because you don't have a clear delimiter, You could possibly use Pattern/Matcher to extract the fields, but only if there is a specific consistent Pattern to the lines in your input file. Again, you haven't given us enough data to go on.
     
    Knute Snortum
    Sheriff
    Posts: 4274
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    bunny Indhrani wrote:Thank you,
    actually my task is to compare two fields in two text files..that is,in both files i have roll_number of a student and when i compare those roll_num in both text files,then if they are same then i need to retrieve some info from the text files like student marks. so for that i need to read two files and then compare them by using matcher and pattern classes..
    can you help me out with my above code..


    My confusion is that you wouldn't need Pattern and Matcher to tell if a roll_number is the same. My only thought is that maybe you need to break the line into tokens -- but split() would be better for that.

    Assuming that you have to use Pattern and Matcher and that line is a line from the studentdetails.txt file, you could write this to get the roll_number:

    Of course, you would have to adapt this to your file format and integrate it into your two-files-reading code, but that's the jist of it.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:My confusion is that you wouldn't need Pattern and Matcher to tell if a roll_number is the same. My only thought is that maybe you need to break the line into tokens -- but split() would be better for that.

    @bunny: And I reckon that you actually need to convert these files into objects.

    The main problem here is that we don't know what you've been told to do: If this is a class exercise and you've been told to do something a certain way - eg, using Pattern and Matcher - then obviously you must; but it seems to me you're concentrating too much on the mechanics of the job, rather than explaining what needs to be done.

    Java 101: Don't decide on the "how" until you know (or can explain) the "what".

    Winston
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!