Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap and Input/Out Put file

 
Mulugeta Maru
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help to give me direction. I am not sure how I approach this school project. What I want to do is to write student data in a text file and be able to read the file into a HashMap. The two classes I am using are as follows:
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
public class University
{
private HashMap schools;
private HashMap students;

/**
* Constructor for objects of class University.
*/
public University()
{
students = new HashMap();

public void setEnrolStudentInSchool(String schoolCode, String studentId, String name, String e_MailAddress, double secondarySchoolAverage)
{
students.put(studentId.toLowerCase(), new Student(studentId.toLowerCase(), name, e_MailAddress, secondarySchoolAverage, schoolCode));
}
}
public class Student
{
private String studentId;
private String name;
private String e_MailAddress;
private double secondarySchoolAverage;
// School code where the student is enrolled.
private String schoolCode;

public Student(String studentId, String name, String e_MailAddress,
double secondarySchoolAverage, String schoolCode)
{
this.studentId = studentId;
this.name = name;
this.e_MailAddress = e_MailAddress;
this.secondarySchoolAverage = secondarySchoolAverage;
this.schoolCode = schoolCode;
}
}
Thanks
Mulugeta
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing we have to do is clarify your problem. Here's my interpretation: You have a file with student data in it, one student per line.
You want to read a line from that file, make a Student object, and then store the Student object in a HashMap. Each student has a studentId and that studentID is to be used as the key when storing the Student object in the HashMap.
Is that correct?
Better still post the original assignment description.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about some code samples from sun?
 
Mulugeta Maru
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,
Your interpretation of my problem is correct.
Thank you very much.
Mulugeta
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a good idea, William. Here's one of those:

Mulugeta, that's going to help you read the file in. Create a text file containing a few Students' data. And let this program read and print the contents. After you have done that we can convert the Strings read in into Student objects.
Try to find out about the java.util.StringTokenizer class, you will need that too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic