Welcome to Dream.In.Code
Become a Java Expert!

Join 149,477 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,785 people online right now. Registration is fast and FREE... Join Now!




ArrayLists and JOptionPane Input

 
Reply to this topicStart new topic

ArrayLists and JOptionPane Input

Rhyotion
16 May, 2007 - 06:35 PM
Post #1

New D.I.C Head
*

Joined: 23 Aug, 2006
Posts: 1


My Contributions
I'm having trouble with a Java program. It involves a grade book in which I have created a public class named Student.

I have a user enter four grades for each student they want to enter. These grades are saved as doubles in an ArrayList in the Student class. I then have an ArrayList of Students.

Too get user input, I use an JOptionPane in a for loop programmed to iterate 4 times. All these values are added to a
temporay ArrayList of grades, then send that data to a new Student constructor, initializing the temporary grade book to the Student's variable. Then I clear(); the temporary grade book, fresh for a new Student.

However my program takes the last temporary grade book data, and applies them all to different Student's even though other data entered stays the same. Like a String for their name.

I don't have the code tonight, but I can get it posted tomorrow.

Thanks.

Edit: Changed title. ~V

Edit: Here's the Code

CODE
        ArrayList<Student> students = new ArrayList();
        ArrayList<Double> grades = new ArrayList();
        
        String input = JOptionPane.showInputDialog("How many students are you entering:");
        int total = Integer.valueOf(input);
        
        for(int a = 1; a <= total; a++)
        {
                                                grades.clear();

            input = JOptionPane.showInputDialog("Student #" + a + " Name:");
            String name = input;
            
            input = JOptionPane.showInputDialog("Student #" + a + " ID:");
            String ID = input;
            
            for(int b = 1; b <= 4; b++)
            {
                input = JOptionPane.showInputDialog("Test Grade #" + b + ":");
                double num = Double.valueOf(input);
                grades.add(num);  
            }
        
            students.add(new Student(name, ID, grades));
        }


This post has been edited by Rhyotion: 17 May, 2007 - 07:21 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:07PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month