*
The moose likes Beginning Java and the fly likes HashMap and Input/Out Put file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap and Input/Out Put file" Watch "HashMap and Input/Out Put file" New topic
Author

HashMap and Input/Out Put file

Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

How about some code samples from sun?


Please ignore post, I have no idea what I am talking about.
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
Hi Barry,
Your interpretation of my problem is correct.
Thank you very much.
Mulugeta
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.
 
Consider Paul's rocket mass heater.
 
subject: HashMap and Input/Out Put file