[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating a csv file from a map

Posted on 2011-10-17
8
Medium Priority
?
176 Views
Last Modified: 2012-05-12
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...
0
Comment
Question by:gaugeta
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36979568
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36979777
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 36979864
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1000 total points
ID: 36984061
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
 

Author Comment

by:gaugeta
ID: 36984432
@for_yan & @CEHJ:Thanks a lot for you replies.
0
 

Author Comment

by:gaugeta
ID: 36984435
@for_yan & @CEHJ:Thanks a lot for your replies.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36984446
You are always welcome.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36984782
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question