Creating a csv file from a map

Hi Experts...
I have a map of the type LinkedHashMap<String,Integer>  containing the students name and their marks as String and int data.
How do i create a csv file with the students name and their marks displayed one below the other with the header being two columns as Student and Marks.
Please help...
gaugetaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
For something simple like this you can 'roll your own' but otherwise use a proper csv writer. Try something like
PrintWriter out = new PrintWriter(new FileWriter("students.csv"));
final String SEP = System.getProperty("line.separator");
out.printf("%s,%s%s", "Student", "Mark", SEP);
Set<Map.Entry<String, Integer>> marks = map.entrySet();
for(Map.Entry<String, Integer> e : marks) {
   out.printf("%s,%s%s", e.getKey(), e.getValue().toString, SEP);
}
out.close();

Open in new window

0
for_yanCommented:
HahsMap<String, Integer> map = new HashMpa<Strng, Integer)();
map.add("Browm",5);
map.add("Jones",4);

PrintWriter out = new PrintWriter(new FileWriter("marks.csv"));
out.println("Students, Marks";
for(String s : map.keySet()){
out.println(s + "," + map.get(s));

}
out.close();

Open in new window

0
CEHJCommented:
Actually, more code, but more efficient would be
PrintWriter out = new PrintWriter(new FileWriter("students.csv"));
	out.print("Student");
	out.print(",");
	out.println("Mark");
	Set<Map.Entry<String, Integer>> marks = map.entrySet();
	for(Map.Entry<String, Integer> e : marks) {
	    out.print(e.getKey());
	    out.print(",");
	    out.println(e.getValue().toString());
	}
	out.close();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

for_yanCommented:
Just comopiled and ran the code to
eliminate misprints:
public class WriteCsvFile {
public static void main(String [] args) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("Browm",5);
map.put("Jones",4);
map.put("Smith",3);
         try{
PrintWriter out = new PrintWriter(new FileWriter("students_marks.csv"));
out.println("Students, Marks");
for(String s : map.keySet()){
out.println(s + "," + map.get(s));

}
out.close();
         }catch(Exception ex){
             ex.printStackTrace();
         }
}

}

Open in new window



students-marks.csv
0
gaugetaAuthor Commented:
@for_yan & @CEHJ:Thanks a lot for you replies.
0
gaugetaAuthor Commented:
@for_yan & @CEHJ:Thanks a lot for your replies.
0
for_yanCommented:
You are always welcome.
0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.