Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Need help storing user data in hashtable

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Need help storing user data in hashtable

    Hey, I am making a class registering program where the user can create course add students to that class and some other things. I am having trouble with the hashtable. I am trying to store the user created class in the hashtable. Here is what I got so far.
    Here is my view package:user input class
    ....... else if(mainMenu.equalsIgnoreCase("add course")){
    				String newID = JOptionPane.showInputDialog("Type course ID number ");
    				String newCourse = JOptionPane.showInputDialog("Type course name ");
    				Course cour = new Course (newID,newCourse);
    JOptionPane.showMessageDialog(null,"course" + cour.getIndex() + cour.getName() + " has been successfully added" );

    Here is model package with course class
    I tested to see if this class gets the user input and it does since return the index and name I type but when I add the line
    // create an object
        			Course mtd1 = new Course(index, name);
     
        			// add it to the Hashtable using its id as the key
        			enrollments.put(mtd1.getIndex(), mtd1);

    I get an error Exception in thread "main" java.lang.StackOverflowError
    at java.util.Dictionary.<init>(Unknown Source)
    at java.util.Hashtable.<init>(Unknown Source)
    at java.util.Hashtable.<init>(Unknown Source)
    at name.xxxxxxx.model.Course.<init>(Course.java:17)
    .....
    What am I doing wrong with the hashtable?
    thanks

    package name.xxxxxxxxx.model;
    import java.awt.Component;
    import java.util.Enumeration;
    import java.util.Hashtable;
    public class Course {
    	String index;
    	String name;
    	// store roster list
    		Hashtable enrollments;
     
     
        public Course(String index, String name){
        	this.index = index;
        	this.name = name;
     
        	// create an empty Hashtable
        			Hashtable enrollments = new Hashtable();
     
        			// create an object
        			Course mtd1 = new Course(index, name);
     
        			// add it to the Hashtable using its id as the key
        			enrollments.put(mtd1.getIndex(), mtd1);
     
     
     
    	 }
     
     
     
     
    	public String getIndex() {
    		return index;
    	}
     
    	public String getName() {
    		return name;
    	}


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need help storing user data in hashtable

    You've got an almost infinite recursion going on. The Course object will create a new Course object in its constructor which creates a new Course object in its constructor which creates a new Course object in its constructor which creates a new Course object in its constructor which creates a new Course object in its constructor which creates a new Course object in its constructor which creates .... you get the picture, and this goes on until the JVM runs out of stack memory and you see a StackOverflowError.

    Solution: don't do this. Don't have the constructor create a new Course object.

    Note: you never replied to replies in your previous question. Consider replying to those folks now, even to say that you solved it and how you solved it. It's basic human nature to like to feel like one's efforts have been at least acknowledged.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    leonne (November 4th, 2012)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Need help storing user data in hashtable

    ok thanks, ill try that out, and I replied to my other post.

    ok here is what I did, seems to work well no error.
    would this way work? The program should let the user create as many course has they want and store them.
    public Course(Hashtable enrollments){

    // create an empty Hashtable
    //Hashtable enrollments = new Hashtable();

    // create an object
    Course mtd1 = new Course(index, name);

    // add it to the Hashtable using its id as the key
    enrollments.put(mtd1.getIndex(), mtd1);
    }

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need help storing user data in hashtable

    Also, please let me know what exactly the Hashtable is supposed to hold?

    I'm guessing that it will hold Student objects, and if so, and since it is held by a Course object, there's no need to put a Course object into it. Note that your code is also guilty of variable "shadowing" in that you re-declare your Hashtable variable inside of the constructor. This means that the Hashtable field declared in the class is never initialized and remains null.

    Also, why use Hashtable instead of the preferred HashMap? Per the Java Hashtable API:
    As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.

  6. #5
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Need help storing user data in hashtable

    Professor told us that we need to use hashtable. Here is what he told us for student class"...Hashtable variable, enrollments to hold all enrollment of this stduent." Also said for the course class "Add a Hashtable variable, enrollments to hold all enrollment of this course. " Im Kind of confused, It also says "
    6. Add Hashtable variable, such as students, to hold all Student objects, to StudentRecords class.
    7. Add Hashtable variable such as courses to hold all Course objects, to CourseOfferings class.
    So, there needs to be 3 hashtable right? Course to store all the classes student stores all students and enrollment to store, all the course that one student is enrolled in right? So that means I did it wrong cause right now what I wrote stores created course in enrollment.

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need help storing user data in hashtable

    Do you have your instructions printed out? If so, post them verbatim here, because I too am confused. Also, I would not pass a Hashtable object into the Courses constructor. Create a Hashtable in courses, just be careful not to shadow the field, not to re-declare the field as a variable local to a constructor or method.

  8. #7
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Need help storing user data in hashtable

    its a txt file here
    1. Make sure you have the code in your view class, StudentSystem, to receive all commands listed in the latest Mid-term project requirement document on Sakai. Make sure your program can be compiled and executed after this.
    2. Add String variables such as RUID, first name, last name to Student class. And add getter and setter methods for them. Add a Hashtable variable, enrollments to hold all enrollment of this stduent. Add a constructor with three String parameters for RUID, first name, and last name.
    3. Add String variables such as index, name to Course class. And add getter and setter methods for them. Add a Hashtable variable, enrollments to hold all enrollment of this course. Add a constructor with String parameters for index and name.
    4. Add variable student of Student type, variable course of Course type, and String variable grade to Enrollment class. And add getter and setting methods for them. Add a constructor with Student and Course variable as input parameters. An Enrollment object represent one Student register for one Course with one grade.
    5. Add Hashtable variable, such as students, to hold all Student objects, to StudentRecords class. Add method, addStudent, with three String parameters, RUID, first name, last name to create a Student object and add it into students hashtable with RUID as key. Add method, getStudent, with String parameter, RUID, to find a student in the hashtable, and return the Student object.
    6. Add Hashtable variable such as courses to hold all Course objects, to CourseOfferings class. Add method, addCourse, with two String parameters, index and name to create a Course object and add it into courses hashtable with index as key. Add method, getCourse, with String parameter, index, to find a course in the hashtable, and return the Course object.

  9. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need help storing user data in hashtable

    Quote Originally Posted by leonne View Post
    its a txt file here
    1. Make sure you have the code in your view class, StudentSystem, to receive all commands listed in the latest Mid-term project requirement document on Sakai. Make sure your program can be compiled and executed after this.
    2. Add String variables such as RUID, first name, last name to Student class. And add getter and setter methods for them. Add a Hashtable variable, enrollments to hold all enrollment of this stduent. Add a constructor with three String parameters for RUID, first name, and last name.
    3. Add String variables such as index, name to Course class. And add getter and setter methods for them. Add a Hashtable variable, enrollments to hold all enrollment of this course. Add a constructor with String parameters for index and name.
    4. Add variable student of Student type, variable course of Course type, and String variable grade to Enrollment class. And add getter and setting methods for them. Add a constructor with Student and Course variable as input parameters. An Enrollment object represent one Student register for one Course with one grade.
    5. Add Hashtable variable, such as students, to hold all Student objects, to StudentRecords class. Add method, addStudent, with three String parameters, RUID, first name, last name to create a Student object and add it into students hashtable with RUID as key. Add method, getStudent, with String parameter, RUID, to find a student in the hashtable, and return the Student object.
    6. Add Hashtable variable such as courses to hold all Course objects, to CourseOfferings class. Add method, addCourse, with two String parameters, index and name to create a Course object and add it into courses hashtable with index as key. Add method, getCourse, with String parameter, index, to find a course in the hashtable, and return the Course object.
    OK, it appears that you'll need four Hashtables, one inside of Students and called enrollments, one inside of Course and also called enrollments, one in StudentRecords and called students, and one inside of CourseOfferings called courses.

  10. #9
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Need help storing user data in hashtable

    ok thanks for the help

  11. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Need help storing user data in hashtable

    You're welcome, and best of luck!

  12. #11
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: Need help storing user data in hashtable

    ok question, this is what i did. first class should store all the courses in a hashtable. the next class store all students in a particular class which i am confused about
    import java.util.Hashtable;
    import name.Pamieta.chris.model.Course;
    public class CourseOfferings {
     
    		// store all course objects
    	Hashtable courses;
     
     
    	// add a new course to the course list
    	public Course addCourse(String index, String name){
     
    		// create an object
    		Course mtd1 =  new Course(index, name);
     
    		// add it to the Hashtable using its id as the key
    		return (Course) courses.put(mtd1.getIndex(), mtd1);
     
     
    	}
    	// get course object
    	//Add method, getCourse, with String parameter, index, to find a course in the hashtable, and return the Course object.
    	 public Course getCourse(String index){
    	return (Course) courses.get(index);

    heres the class our professor told us this is where u store students in the class. seems like i need to use "// add an enrollment to roster list
    public Enrollment addEnrollment(Enrollment e) {" but i dont know what the e is in enrollment e thought i would do something similar in the top class but i guess not and nothing in the enrollment class has an e. Am I on the right track or completely off?

    public class Course {
    	String index;
    	String name;
    	// store roster list
    		Hashtable enrollments;
     
     
        public Course(String index, String name){
        	this.index = index;
        	this.name = name;
     
     
     
     
     
        }
     
    	public String getIndex() {
    		return index;
    	}
     
    	public String getName() {
    		return name;
    	}
     
     
    	// add an enrollment to roster list
    	public Enrollment addEnrollment(Enrollment e) {
     
    		// create an object
    				Enrollment mtd1 =  new Enrollment(e);
     
    				// add it to the Hashtable using its id as the key
    				return (Course) courses.put(mtd1.getIndex(), mtd1);
     
     
     
     
     
    	// remove an enrollment from roster list
    	public Enrollment removeEnrollment(String RUID) {
     
    		return (Enrollment) enrollments.remove(RUID);
     
    	}
    		// assign a grade to a student through 
    	// roster list
    	public Enrollment grade(String RUID, String grade){
    	}
     
     
     
    }

    public class Enrollment {
    	Student student;
    	Course course;
    	String grade;
     
     
    	public Enrollment (Student studentS, Course courseC ){
    		student = studentS;
    		course = courseC;
     
    	}
    	public void setCourse(Course course) {
    		this.course = course;
    	}
    	public Course getCourse() {
    		return course;
    	}
     
    	public void setGrade(String grade) {
    		this.grade = grade;
    	}
    	public String getGrade() {
    		// when assign a grade to a student
    		// you also need to recalculate GPA
     
    		return grade;
    	}
     
    	public void setStudent(Student student) {
    		this.student = student;
    	}
    	public Student getStudent() {
    		return student;
    	}
     
    }

Similar Threads

  1. Storing data
    By imsuperman05 in forum Collections and Generics
    Replies: 0
    Last Post: December 23rd, 2011, 05:36 PM
  2. Storing data from a file in array and setting it to textbox
    By macko in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 13th, 2011, 09:17 PM
  3. Storing User Data
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: January 21st, 2011, 09:07 AM
  4. storing data using form
    By anupam.j2ee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2010, 10:43 AM
  5. Storing data
    By Joyce in forum Collections and Generics
    Replies: 1
    Last Post: September 20th, 2010, 09:16 AM