Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By ravi.joshi53

Thread: Writing conditional tab separated values using FileWriter

  1. #1
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Question Writing conditional tab separated values using FileWriter

    I am reading values from some files and my objective is to write back those values in specified format given below-


    STUDENT_NAME SUBJECT_NAME OTHER
    tom physics TRUE
    bob physics
    jhon chemistry
    steve mathematics
    tom chemistry
    mark history
    tom history


    All the columns are tab separated. I already have all the values of STUDENT_NAME and their corresponding SUBJECT_NAME.
    The task is to prepare a report as tab separated fields and if any student has more than one SUBJECT_NAME, put TRUE in the third column only for the first occurrence of STUDENT_NAME. In the above example, tom have three subjects, physics, chemistry and history. So the first occurrence is marked as TRUE. I am unable to design algorithm for this. As far as implementation part is concerned, tab separated file can easily be created using FileWriter. At the time of writing these values just I need to put \t char to make it tab separated file. What about the last column? How can I write my FileWriter to satisfy my above condition?
    Last edited by ravi.joshi53; 09-08-2012 at 02:46 PM. Reason: Changed formatting

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Writing conditional tab separated values using FileWriter

    Would you like to try out some more colors that may be even more difficult to read?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    hey db, you got the objective very well. I changed the color. Can you please help me now?
    DarrylBurke likes this.

  4. #4
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    Quote Originally Posted by ravi.joshi53 View Post
    Can you please help me now?
    Hardly an 'advanced Java' task. It is difficult to help without seeing what you have already done. Or are you just expecting someone to post the completed assignment so you can plagiarise it?
    Last edited by sabre150; 09-09-2012 at 07:56 AM.

  5. #5
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    Hi sabre150,
    Initially i was using Hashset<String> to store new String(STUDENT_NAME+SUBJECT_NAME) in it. Because in my output data, i don't want any repetition of STUDENT_NAME with same SUBJECT_NAME. Below is the code snippet-

    Java Code:
    HashSet<String> info=new HashSet<String>();
    
    for (File file : allFiles) {
    /*
     * Here i am reading file and extracting value of STUDENT_NAME and SUBJECT_NAME parameters and 
     * assigning STUDENT_NAME to String student, SUBJECT_NAME to String subject
     */
    
    /*
     * add STUDENT_NAME+SUBJECT_NAME to HashSet to avoid duplication of STUDENT_NAME+SUBJECT_NAME
     */
    	info.add(student+subject);
    }
    
    /*
     * Now using FileWriter write this HashSet to output file.
     */
    
    try {
    	FileWriter fw = new FileWriter(new File(output));
    
    	Iterator<String> studentInfo = info.iterator();
    	while (studentInfo.hasNext())
    		fw.write(studentInfo.next()+'\n');
    	fw.close();
    } catch (IOException e) {
    	System.err.println(e.getMessage());
    }
    But i am not able to apply my condition, which is if any student has more than one SUBJECT_NAME, put TRUE in the third column only for the first occurrence of STUDENT_NAME.

  6. #6
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    are you just expecting someone to post the completed assignment so you can plagiarise it?
    No, Never. I haven't told it. The reason, why i haven't posted any code snippet for the question is that i am not looking for code. Read my question again. It is clearly written
    I am unable to design algorithm for this
    I Now I have posted my code snippet in above comment. You can go through it. Thanks in advance.

  7. #7
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    Seems to me you need a Map mapping student name to a set of subjects. You can go though all the files adding an entry to the map if that name is not already in the map and updating the entry if it is. I would define a class to be used as a container of the set since this gives one the maximum flexibility to add any other attributes.

  8. #8
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    Quote Originally Posted by ravi.joshi53 View Post
    No, Never. I haven't told it. The reason, why i haven't posted any code snippet for the question is that i am not looking for code. Read my question again. It is clearly written

    I Now I have posted my code snippet in above comment. You can go through it. Thanks in advance.
    Sorry but your question is not "clearly written" and looks to be a precis of the assignment given by your teacher/lecturer. The belated code fragment you have posted does little; it does not even parse the files you are reading. It is a waste of time trying to write anything using a FileWriter until you can read the files and build the map.
    Last edited by sabre150; 09-09-2012 at 08:53 AM.

  9. #9
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    Thank you sabre150 for the fast reply. Ya, you are right. FileWriter part is not a big deal.
    Now in Mapping part, i have to use one to many mapping. am i right? Every student can have more than one subject. And every student has an unique name (in this case).
    Correct me if i am wrong above.

  10. #10
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    From what you have posted you are right but still jumping ahead of yourself. Like any programming project you need to do this in stages. For example -
    1) Develop code to read the files.
    2) Develop code to parse the files.
    3) Design an object to hold the information for each student.
    4) Build the map.
    5) Output the map.

  11. #11
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Writing conditional tab separated values using FileWriter

    Correct. I will manage all. I was just worried about the idea. Now using map i can implement it easily. I will let you know if at all face any doubts. Thank you.

Similar Threads

  1. Reading a comma separated file into JTextPane
    By arndtmatt in forum New To Java
    Replies: 3
    Last Post: 04-09-2012, 10:08 PM
  2. Problems with writing String values to a text file
    By Tedward in forum New To Java
    Replies: 8
    Last Post: 06-02-2011, 04:28 AM
  3. editing jTable then writing new values to database!
    By sweet angle in forum AWT / Swing
    Replies: 6
    Last Post: 01-27-2009, 05:07 PM
  4. writing array values to another class
    By ronald christian in forum New To Java
    Replies: 27
    Last Post: 11-07-2008, 04:08 PM
  5. Replies: 0
    Last Post: 04-16-2008, 10:58 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
  •