Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default .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?

    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
    Any info is much appreciated.

    Cheers,

    Sean..
    Last edited by ManInTheMiddle; 04-13-2012 at 04:05 PM.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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) );

  3. #3
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: .txt & binary .dat file help

    will that work to implement an if statement to do that maths on it?

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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 ;)

  5. #5
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default 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

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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?

  7. #7
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default 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.

  8. #8
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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.

  9. #9
    Join Date
    Mar 2012
    Location
    Dublin
    Posts
    21
    Rep Power
    0

    Default Re: .txt & binary .dat file help

    Cheers, nice one dude. I'll give that a go.

    Much appreciated..

Similar Threads

  1. Problem with file BufferedReader using binary file.
    By allymoha in forum Advanced Java
    Replies: 6
    Last Post: 12-16-2011, 10:01 AM
  2. reading a from a binary file
    By trishtren in forum New To Java
    Replies: 2
    Last Post: 05-11-2011, 06:49 PM
  3. Image file to binary file conversion
    By Mantra in forum New To Java
    Replies: 5
    Last Post: 11-29-2010, 01:59 PM
  4. update binary file from text file
    By billdef in forum New To Java
    Replies: 8
    Last Post: 09-02-2010, 10:24 AM
  5. Replies: 3
    Last Post: 05-10-2009, 12:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •