4 Replies - 84 Views - Last Post: 06 August 2012 - 05:50 AM Rate Topic: -----

#1 udnib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-July 12

writing and appending to file

Posted 06 August 2012 - 05:16 AM

My input file includes data from both males and females. My code below replaces a 'male string' with the key and values and writes into an existing file for 'male'. How can I do the same for females and write into a different 'female string' in the same program?

Sorry If I am confusing you!!! But I want something like

"going through my input----if it is a 'male', replace the 'male string' with corresponding key and values-------else if a 'female', replace the 'female string' with corresponding key and values". Thank You.

Map<String, List<String>> MaleMap = new LinkedHashMap<String, List<String>>();
Map<String, List<String>> FemaleMap = new LinkedHashMap<String, List<String>>();
try {
        Scanner scanner = new Scanner(new FileReader(
                ":C/"));

        while (scanner.hasNextLine()) {
            String nextLine = scanner.nextLine();
            String[] column = nextLine.split(":");
            if (column[0].equals("Male") && (column.length == 4)) {
                MaleMap.put(column[1],
                        Arrays.asList(column[2], column[3]));
            } else if (column[0].equals("Female") && (column.length == 4)) {
                FemaleMap.put(column[1],
                        Arrays.asList(column[2], column[3]));
            }
        }
        Set<Entry<String, List<String>>> entries = MaleMap.entrySet();
        Iterator<Entry<String, List<String>>> entryIter = entries
                .iterator();
        while (entryIter.hasNext()) {
            Map.Entry entry = (Map.Entry) entryIter.next();
            Object key = entry.getKey(); // Get the key from the entry.

            List<String> value = (List<String>) entry.getValue();
            Object value1 = " ";
            Object value2 = " ";
            int counter = 0;
            for (Object listItem : (List) value) {
                Writer writer = null;
                Object Name = key;
                Object Age = null;
                Object ID = null;
                if (counter == 0) {// first pass assign value to value1
                    value1 = listItem;
                    counter++;// increment for next pass
                } else if (counter == 1) {// second pass assign value to value2
                    value2 = listItem;
                    counter++;// so we dont keep re-assigning listItem for further iterations
                }
            }
            System.out.println(key + ":" + value1 + "," + value2);
            scanner.close();
            Writer writer = null;
            Object Name = key;
            Object Age = value1;
            Object ID = value2;
            try {
                String filename = ".txt";
                FileWriter fw = new FileWriter(filename, true); // the true will append the new data
                fw.write("# Table" + Name + "\n" + "map:"
                        + Name + " a :ClassMap;" + "\n"
                        + " :dataStorage map:database;" + "\n"
                        + " :uriPattern " + Name + "/@@ "
                        + Age + "." + ID + "@@;" + "\n"
                        + " :class :" + Name);// appends the string to the file
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    } catch (Exception e) {
        e.printStackTrace();

    }
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: writing and appending to file

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: writing and appending to file

Posted 06 August 2012 - 05:21 AM

":C/" this is the root of your disk, this is not a file name
You cannot expect a Scanner to read that as a file

*Edited: sorry ":C/" is not even "C:/" what is it ?

This post has been edited by pbl: 06 August 2012 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 udnib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-July 12

Re: writing and appending to file

Posted 06 August 2012 - 05:26 AM

View Postpbl, on 06 August 2012 - 05:21 AM, said:

":C/" this is the root of your disk, this is not a file name
You cannot expect a Scanner to read that as a file

*Edited: sorry ":C/" is not even "C:/" what is it ?
Oh sorry. My input file will replace that in my code.

View Postudnib, on 06 August 2012 - 05:25 AM, said:

View Postpbl, on 06 August 2012 - 05:21 AM, said:

":C/" this is the root of your disk, this is not a file name
You cannot expect a Scanner to read that as a file

*Edited: sorry ":C/" is not even "C:/" what is it ?
Oh sorry. Its a typo error. My input file will replace that in my code.

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8029
  • View blog
  • Posts: 31,164
  • Joined: 06-March 08

Re: writing and appending to file

Posted 06 August 2012 - 05:41 AM

Seems complicated for nothing to me. You are over using generic list for nothing I would simply

Map<String, String[]> maleMap = new LinkedHashMap<String, String[]>;
Map<String, String[]> femaleMap = new LinkedHashMap<String, String[]>;

if(column.lengt == 4) {
   if(column[0].equals("male"))
       maleMap.put(column[1], column);
   else if(column[0].equals("female")
       femaleMap.put(column[1], column);
}


no need to create other List by creating a new List object. Use the array already existing

When you'll read back just read the data (array of 4 String)
Just ignore column[0] and column[1] will be your key

That's what I would do
Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: writing and appending to file

Posted 06 August 2012 - 05:50 AM

What is confusing is that you use uppercase starts to your variable names. That makes your code difficult to read. Variables should start lower case in Java.
Always in these cases post a sample of your input file

Your Maps will fail on duplicate name values. You should probably be using ID as the keys

This post has been edited by g00se: 06 August 2012 - 06:01 AM
Reason for edit:: Map problems

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1