• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read data from .dat file and compare the elements of JTable?  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to everyone this is my first ever question on coderanch.

I am creating a billing app. I have two JTables inventory and invoice of DefaultTableModel. My inventory table is connected to a dat file which store the user's data. I want my invoice table to read data from that file compare that the item name present in the invoice table matches inventory table or not. If yes,it proceeds. If no, it displays a warning. How can I do this?

NOTE:- The name of the items are on column 1 of each  table.
 
Saloon Keeper
Posts: 4491
112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far, and where are you stuck making progress?

I suggest to start with the code that reads the file, and then stores it into appropriate data structures. Only once that works, think about how to structure the GUI.
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:What do you have so far, and where are you stuck making progress?

I suggest to start with the code that reads the file, and then stores it into appropriate data structures. Only once that works, think about how to structure the GUI.



I am stuck at the start how to read and much more how to compare. Please help me.
 
Ranch Hand
Posts: 506
12
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is this action to compare triggered? What window (with which table) and listener starts this?
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasad Saya wrote:How is this action to compare triggered? What window (with which table) and listener starts this?



There is a button which when pressed compares invoice table with .dat file and it is JInternalFrame.
 
Tim Moores
Saloon Keeper
Posts: 4491
112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am stuck at the start how to read


Have you done Java file I/O before? If not, start reading here: https://docs.oracle.com/javase/tutorial/essential/io/

What does the file format look like?
 
Marshal
Posts: 59169
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Tim is right. You should have a method of reading the .dat file; that should be done before you write the GUI. Separate that out into your business logic. If the .dat file is in text format, you can probably use a Scanner object with method calls like next(), nextint() and nextBigDecimal() depending on the structure of the data. Then you can create an Invoice object.
Beware: A scanner reads the file from line 1 to line 8394456983465937845 and doesn't go to a particular line in the middle of the file, so you may have to read the whole of the file to find what you are seeking.
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome again

Tim is right. You should have a method of reading the .dat file; that should be done before you write the GUI. Separate that out into your business logic. If the .dat file is in text format, you can probably use a Scanner object with method calls like next(), nextint() and nextBigDecimal() depending on the structure of the data. Then you can create an Invoice object.
Beware: A scanner reads the file from line 1 to line 8394456983465937845 and doesn't go to a particular line in the middle of the file, so you may have to read the whole of the file to find what you are seeking.



I don't think it is in simple text data. Here's the content of th dat file

 
Prasad Saya
Ranch Hand
Posts: 506
12
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aayush Gupta wrote:I am creating a billing app. I have two JTables inventory and invoice of DefaultTableModel. My inventory table is connected to a dat file which store the user's data. I want my invoice table to read data from that file compare that the item name present in the invoice table matches inventory table or not. If yes,it proceeds. If no, it displays a warning. How can I do this?
NOTE:- The name of the items are on column 1 of each  table.



Get data from the inventory dat file:
Read data from inventory dat file one record (item details) at a time - using Java IO or NIO2 package classes

Get data from the invoice table:
DefaultTableModel model = (DefaultTableModel) invoiceTable.getModel();
The DefaultTableModel has methods to read table's row and column values or get as a collection; e.g. getDataVector(), etc.

you should be able to compare now.
 
Prasad Saya
Ranch Hand
Posts: 506
12
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't think it is in simple text data. Here's the content of th dat file ...



What is the source of this "dat" file? How is it created?
 
Tim Moores
Saloon Keeper
Posts: 4491
112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That file format looks like serialized Java objects, in which case you will need to find a description of what was serialized in it, and how. Or, much better, find the code that wrote it - if it contains classes other than those in the Java class libraries, you will need those as well.

Be aware that serializing Java objects can fail if the JVMs doing the writing and reading are not of the same version. (That's why serialization should not be used for long-term storage.)
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aayush Gupta wrote:Hi to everyone this is my first ever question on coderanch.

I am creating a billing app. I have two JTables inventory and invoice of DefaultTableModel. My inventory table is connected to a dat file which store the user's data. I want my invoice table to read data from that file compare that the item name present in the invoice table matches inventory table or not. If yes,it proceeds. If no, it displays a warning. How can I do this?

NOTE:- The name of the items are on column 1 of each  table.



I have managed to read data from the file and compare also. If the item is not found it is showing message as I want but if the item is found also it showing that item not found.

Here's the code-
 
Tim Moores
Saloon Keeper
Posts: 4491
112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if (a.equals(fis))


This line does not make sense. Nothing in the file can be equal to the FileInputStream. You need to read an item from "ois" (which is currently unused) and compare it to that.
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:

if (a.equals(fis))


This line does not make sense. Nothing in the file can be equal to the FileInputStream. You need to read an item from "ois" (which is currently unused) and compare it to that.



No it didn't helped same problem
 
Tim Moores
Saloon Keeper
Posts: 4491
112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the code you're now using.
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Post the code you're now using.



The whole code
 
Tim Moores
Saloon Keeper
Posts: 4491
112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code that deals with reading the file, just like you did with the previous code.
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:The code that deals with reading the file, just like you did with the previous code.



I didn't understand the code that reads the file is included in the previous code
 
Sheriff
Posts: 23513
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aayush Gupta wrote:I didn't understand the code that reads the file is included in the previous code



You posted some code which doesn't work. You were given a suggestion about how to change it. You implied that you had changed it (by saying "it didn't helped same problem").

So. Did you change it and have a problem with the new code? If so then pointing at the code you posted earlier is useless -- we need to see the new code.

 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Aayush Gupta wrote:I didn't understand the code that reads the file is included in the previous code



You posted some code which doesn't work. You were given a suggestion about how to change it. You implied that you had changed it (by saying "it didn't helped same problem").

So. Did you change it and have a problem with the new code? If so then pointing at the code you posted earlier is useless -- we need to see the new code.



Ok but I just changed a word

 
Paul Clapham
Sheriff
Posts: 23513
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:You need to read an item from "ois" (which is currently unused) and compare it to that.



Tim meant, "You need to read an item from 'ois' and compare it to that item." Not, "You need to read an item from 'ois' and compare it to 'ois'."

Notice the first part: "You need to read an item from 'ois'" -- you still aren't doing that.
 
Campbell Ritchie
Marshal
Posts: 59169
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . Not, "You need to read an item from 'ois' and compare it to 'ois'." . . . .

Nor does comparing that object to fis help. That looks as if you were guessing when you change your code.
Also, did you find out how that .dat file was created? As you were told yesterday, you can only retrieve a serialised object if you know which class it was created from.
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Tim Moores wrote:You need to read an item from "ois" (which is currently unused) and compare it to that.



Tim meant, "You need to read an item from 'ois' and compare it to that item." Not, "You need to read an item from 'ois' and compare it to 'ois'."

Notice the first part: "You need to read an item from 'ois'" -- you still aren't doing that.



Can you tell me how?please
 
Campbell Ritchie
Marshal
Posts: 59169
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't an ObjectInputStream have a readObject() method or similar? Use that. It will return an Object which one would usually cast, but you don't know its runtime type. Start by getting the Object returned and simpply print it with System.out.println().
You have been told that you won't successfully read anything from your file until you know how it was written. You need that information from whoever created the file.
 
Paul Clapham
Sheriff
Posts: 23513
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aayush Gupta wrote:Can you tell me how?please



Here's a simple example: Java object serialization - Tutorial.
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Doesn't an ObjectInputStream have a readObject() method or similar? Use that. It will return an Object which one would usually cast, but you don't know its runtime type. Start by getting the Object returned and simpply print it with System.out.println().
You have been told that you won't successfully read anything from your file until you know how it was written. You need that information from whoever created the file.




this method is writing into the file.
 
Rancher
Posts: 3068
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignore this
 
Aayush Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Ignore this


What do you mean?
 
Norm Radder
Rancher
Posts: 3068
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What do you mean?


Nothing.  That post should be ignored.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!