Results 1 to 9 of 9
Thread: .txt & binary .dat file help
- 04-13-2012, 02:16 PM #1
Member
- Join Date
- Mar 2012
- Location
- Dublin
- Posts
- 21
- Rep Power
- 0
.txt & binary .dat file help
Hi,
I have to write a bookshop stock level type of program.
I have to minus and add the sales (String second) from the stock level (int level) in a book shop. The books data is in a binary .dat file and the sales is in a .txt file. I have the file reading in both files and printing out both of them.
If the ISBN matches I subtract or add it to the original level. I know that's just an if & else statement > or < 0. I need to parse the second to int as its a String to do the Maths on it. Just wondering if anyone can help me place the code?
Any info is much appreciated.Java Code:import java.io.*; import java.util.*; public class LeagueFind { public static void main(String[]args){ try{ RandomAccessFile in = new RandomAccessFile("books.dat", "r"); Scanner in2 = new Scanner(new File("transactions.txt")); String first, second; System.out.println(); System.out.printf("%-10s %-15s %-30s %s \n", "ISBN", "AUTHOR", "TITLE", "STOCK LEVEL"); while(in.getFilePointer() < in.length()){ String author = in.readUTF(); String title = in.readUTF(); String isbn = in.readUTF(); int level = in.readInt(); System.out.printf("%-10s %-15s %-30s %d \n", isbn, author, title, level); } System.out.println(); System.out.printf("%10s %10s \n", "ISBN", "SALES"); while(in2.hasNext()){ first = in2.next(); second = in2.next(); System.out.printf("%10s %10s \n", first, second); } in2.close(); in.close(); } catch(IOException e){ System.out.print("fail"); } } } books.dat ISBN AUTHOR BOOK STOCK LEVEL 0201403765 Jan Skansholm Ada 95 from the Beginning 100 0202535665 M. Ben-Ari Software Engineering 25 034565976X Michael Feldman Program Construction 12 080539057X M.A. Weiss Data Structures 30 0805645782 Ken Arnold Java for Programmers 10 0905297568 A. Badone Chaos Theory 15 transactions.txt ISBN SALES 0201403765 -55 0201403765 2 0202535665 10 0202535665 -28 034565976X -7 080539057X -15 0905297568 13 0905297568 -5
Cheers,
Sean..Last edited by ManInTheMiddle; 04-13-2012 at 03:05 PM.
- 04-13-2012, 02:23 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: .txt & binary .dat file help
Are you looking for Integer.parseInt(String)?
as in
System.out.printf("%10s %6d \n", first, Integer.parseInt(second) );
- 04-13-2012, 02:26 PM #3
Member
- Join Date
- Mar 2012
- Location
- Dublin
- Posts
- 21
- Rep Power
- 0
Re: .txt & binary .dat file help
will that work to implement an if statement to do that maths on it?
- 04-13-2012, 02:37 PM #4
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: .txt & binary .dat file help
Maybe? If you do the next step and say
int iValue = Integer.parseInt(second);
int iResult = iValue * 456; // Or whatever math you want to do ;)
- 04-13-2012, 02:44 PM #5
Member
- Join Date
- Mar 2012
- Location
- Dublin
- Posts
- 21
- Rep Power
- 0
Re: .txt & binary .dat file help
but the Math has to come from the text file direct, no code. So for example the sales for isbn - 0201403765 were -55 so I have to subtract -55 from the dat file isbn that matches
- 04-13-2012, 02:56 PM #6
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: .txt & binary .dat file help
I doubt you want to subtract -55 from the ISBN - you may rather want to subtract 55 from the corresponding ISBN "STOCK LEVEL", right?
Well then you should implement a method that does this - I have shown you how to convert a string to a number and with numbers you may do the subtraction, right?
After doing that math you have to rewrite the DAT file or better the file, line by line by converting your numbers back to String literals.
You think that way would work?
- 04-13-2012, 03:04 PM #7
Member
- Join Date
- Mar 2012
- Location
- Dublin
- Posts
- 21
- Rep Power
- 0
Re: .txt & binary .dat file help
I meant if the ISBN matches in both files then it will subtract or add the sales to the stock level.
So a method to take in the int level and the sales level subtract or add them, then call it back into the main? We are not to edit the details of the files only "r" them do calculations then print to screen the final results.
- 04-13-2012, 03:21 PM #8
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: .txt & binary .dat file help
Ok, so your challenge is easier - you do not need to rewrite the dat file. Well you could do that now with the information you have.
Just add a line that adds the both int numbers and afterwards you output the result.
You will need to store the contents of ONE of the files at least in memory (use Arrays or ArrayList for that purpose). Usually and best you would do it like that:
Create an own class for an item of the files that has the following attributes:
String author;
String title;
String isbn;
int level;
Then read the file in as an array of objects of that class. Then you loop through this array at each line of the second file and check if it matches the ISBN... if it does you modify the level attribute of that object... after that you close the second file and do in a second loop the printout... you may do this best in a function inside the new class you have created before.
- 04-13-2012, 03:38 PM #9
Member
- Join Date
- Mar 2012
- Location
- Dublin
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Problem with file BufferedReader using binary file.
By allymoha in forum Advanced JavaReplies: 6Last Post: 12-16-2011, 09:01 AM -
reading a from a binary file
By trishtren in forum New To JavaReplies: 2Last Post: 05-11-2011, 05:49 PM -
Image file to binary file conversion
By Mantra in forum New To JavaReplies: 5Last Post: 11-29-2010, 12:59 PM -
update binary file from text file
By billdef in forum New To JavaReplies: 8Last Post: 09-02-2010, 09:24 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks