Results 1 to 4 of 4
  1. #1
    rakster is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default A small question: reading and writing files

    Hello everyone, I'm kind of new as a java user, and i got a little bit confused when i got my last assignament, so i though i could ask for some tips here c:

    My problem is:

    I have to make a code (using Dr. Java) that reads 3 different files (prices.txt, trainprices.txt, costumers.txt) each one containing different data:

    prices.txt has a list of prices, serial code (from 0000 to 9999) and name of different items, goes like this:

    0001,item1,priceitem1
    0002,item2,priceitem2
    000n,itemn,priceitemn

    trainprices.txt has the price of each train, there are 3 kind of trains, going from 0001 to 0020 as train A, from 0021 to 0040 as train C and 0041 to 0060 as train C.

    ex:

    trainA,pricetrainA
    trainB,pricetrainB
    trainC,pricetrainC

    and costumers.txt has the data of every costumer, their ID,name, gender, age, nationality, and all the items they bought

    ex:

    IDnumber1,name1,age1,sex1,natioality1,traincode(fr om 0000 to 0060),nationality1,codeitem(a),codeitem(b),codeite m(c),0
    IDnumber2,name2,age2,sex2,natioality2,traincode(fr om 0000 to 0060),nationality2,codeitem(c),codeitem(z),codeite m(n),codeitem(i),0
    IDnumber3,name3,age3,sex3,natioality3,traincode(fr om 0000 to 0060),nationality3,codeitem(d),codeitem(f),codeite m(k),0
    IDnumber4,name4,age4,sex4,natioality4,traincode(fr om 0000 to 0060),nationality4,codeitem(a),codeitem(k),codeite m(l),codeitem(o),codeitem(m),0

    So, what i have to do is to read those 3 files and make a new one called "payments.txt" that would go like this:

    IDnumer1,Traintype(Acording to the code [A, B, C]),traincode,totaltopay1 (total=price train _ + price of each item bought)
    IDnumer2,Traintype,traincode,totaltopay1
    IDnumer3,Traintype,traincode,totaltopay1

    can someone help me with a tip on how to do it please, i'm not asking for the entire code, just for some advices, thanks ^^

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: A small question: reading and writing files

    Hi rakster, try writing down the steps you need to take to solve the problem(leaving aside the implementation). :)

    BR,
    Kalin

    P.S. If your confusion persist, come back here with more detail about it and describing what you have tried/done :)
    Last edited by kalata; 05-31-2013 at 11:31 AM.

  3. #3
    rakster is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: A small question: reading and writing files

    The steps should be like these:

    • Read item prices
    • read train prices
    • read costumers prices til it ends.
    • with the all the data make a new file called payments


    As far as i tried i can only make the file read only 1 file and from it make the output file, somehow i can't make it get data from the others, the functional one i have goes like this:

    Java Code:
    import disc.*;
    import java.io.*;
    public class Test1{
    public static void main (String[] args) throws IOException{
    ArchivoEntrada f1 = new ArchivoEntrada("costumers.txt");
    ArchivoSalida out = new ArchivoSalida("payments.txt");
    
    while(!f1.isEndFile()){
      Registro line = f1.getRegistro(); //reads specific file
      String id = line.getString(); //gets the ID on the line
      String name = line.getString(); // gets the name on line
      int age = line.getInt(); //gets the age
      String sex = line.getString(); //gets the gender
      String nation = line.getString(); //gets the nationality
      String traincode = line.getString(); //gets the train code
    
    Registro lout = new Registro (5);
    lout.agregarCampo(id); //from last file
    lout.agregarCampo("planetype"); //not defined yet
    lout.agregarCampo(traincode); //writes train code
    lout.agregarCampo("total"); //haven't calculated total yet
    out.writeRegistro(lout);
    }
    a1.close();
    out.close();
    }
    }
    Costumers.txt:

    1811111-2,derp,20,male,spanish,0030,1,3,5,0
    1822222-3,derpo,18,male,british,0030,2,4,5,0
    1833333-4,captainovciou,40,female,chinese,0030,1,4,5,0
    1844444-5,dunno,30,female,japanese,0010,2,3,5,0
    1855555-6,why,90,female,korean,0010,1,4,5,0
    1866666-7,uno,15,female,reptilian,0024,2,4,5,0
    1877777-8,work,17,male,alien,0037,2,3,4,0

    so what i can't make is:
    • Compare the codes on costumers.txt (items) with items.txt and get it's price
    • Read the other files


    for ArchivoEntrada/ArchivoSalida I use disc.jar disc.jar
    i know there should be another command but i use it in spanish :c

    Thanks for replying (:

  4. #4
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: A small question: reading and writing files

    Hi,
    @Read the other files
    for each file for the input (prices.txt, trainprices.txt, costumer.txt(I guess you mean here customer :P)) you will need a separate object of type File. ArchivoEntrada is kind of a wrapper for class File, so... :)
    As an off-topic tip I suggest you to use try/catch/finally blocks when dealing with streams in order to guarantee that in the end all streams are closed(in the finally block), e.g. :
    Java Code:
    InputStream is = null; 
    try {
        // do your stuff with the stream here
    } catch(IOException e) {
    
    } finally {
        try {
            if(is != null) {
                is.close();
            }
        } catch(IOException e) {
            //Something terrible must have happened
        }
    }
    @Compare the codes on costumers.txt (items) with items.txt and get it's price
    Think of a way to store all the read data from the input files for more than one iteration of the loop :)

    BR,
    Kalin

Similar Threads

  1. reading and writing txt. files into a database
    By Dan C in forum New To Java
    Replies: 1
    Last Post: 05-30-2013, 09:51 AM
  2. Reading / Writing files
    By Learning Java in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 10:21 PM
  3. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 04:15 AM
  4. Reading/Writing files through Applet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-15-2007, 09:20 PM

Posting Permissions

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