Making a list of list?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1043 Views - Last Post: 24 August 2010 - 08:13 AM Rate Topic: -----

#1 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Making a list of list?

Posted 22 August 2010 - 01:44 PM

im trying to make a simple gradebook. nothing too difficult, but i want to be able to add names and grades pretty freely, without worrying about filling an array. i went with arraylist since they are pretty easy to remove and add things.

what i have so far is a few prompts that allow you to fill a list with user entered names. the user can continue to enter names until they choose 'n', which breaks the loop.

basically my problem is that once i get the number of students i need to create a loop that will create an arraylist for each student so that i can fill each list with individual grades. i just dont know if this can be done. im not really sure of a better way to do it, but if you guys know one im very open to suggestions.

btw, this isnt an assignment. just something im trying to do to get better.

here is my code so far:
//*********************************************************************************
//    Main.java                   Nathan Gibson
//
//    A simple java gradebook to keep track of students and grades.
//*********************************************************************************

import java.util.*;
public class Main
{
  public static void main(String[]args)
  {
    //initialize Scanner
    Scanner sc = new Scanner(System.in);
    
    //initializes a list for student
    ArrayList students = new ArrayList();
    
    char yes;
    
    do{
      
      System.out.println("Would you like to add a student(y/n)? ");
      String line = sc.nextLine();
      yes = line.charAt(0);
      if(yes == 'y')
      {
        System.out.println("Please enter the student's name");
        String name = sc.nextLine();
        students.add(name);
      }
      else
      { 
        System.out.println("Contents of students: " + students); 
      }
    }while(yes == 'y');

      
      
  }
}


thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Making a list of list?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,802
  • Joined: 27-December 08

Re: Making a list of list?

Posted 22 August 2010 - 01:46 PM

Sounds like parallel arrays with a 2D array. Not good practice at all. Just store a List<Student> and define a Student class to encapsulate List<Double> for grades.

For more information, see my tutorial Moving Away From Parallel Arrays.

Edit: For a reference for syntax, you can create multi-dimmensional Lists by doing List<List<E>> for a 2D List. Same way as for Object[][] for a 2D array.
Was This Post Helpful? 1
  • +
  • -

#3 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Making a list of list?

Posted 22 August 2010 - 01:46 PM

you would simply need a single ArrayList<Student> where you have a student object that holds the names and grades

myArrayList.add(new Student(name, grade)); 


Was This Post Helpful? 1
  • +
  • -

#4 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: Making a list of list?

Posted 22 August 2010 - 02:19 PM

View PostLuckless, on 22 August 2010 - 12:46 PM, said:

you would simply need a single ArrayList<Student> where you have a student object that holds the names and grades

myArrayList.add(new Student(name, grade)); 



Would this work with multiple grades for each student? would i still be able to add or remove and all that good stuff? i looked at alot of stuff for arraylist and didnt see that you could do that anywhere, can you point me to a reference so i can stop bugging you all with questions? thanks.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,802
  • Joined: 27-December 08

Re: Making a list of list?

Posted 22 August 2010 - 02:22 PM

If you define a Student class per my last post, you can get() a Student from the List, and modify it accordingly. No reason why you can't define Student to encapsulate a List<Double> for grades and provide access to its methods by defining your own.

As an example:
class Student{
  private List<Double> grades;

  public boolean addGrade(double d){return grades.add(d);}

}


Was This Post Helpful? 1
  • +
  • -

#6 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: Making a list of list?

Posted 22 August 2010 - 05:53 PM

thanks for the help you guys, im still working on the same thing, but shifting priorities a little bit. im trying to get my methods all in check(what im more confident doing) and waiting till i have more time to work on the stuff that will be more difficult.

my problem is that i cant get one of my methods to work. im probably looking over something.

public void printStudents()
  {
    System.out.println(students); 
  }


and im calling it here:

if(selection == 1)
      {
        group1.addStudent();
        group1.printStudents();
      
      }


basically what im trying to do is let the user update their stuff and then when they are done it will print an update of what they were editing.

i have two seperate classes, here they are:

//*********************************************************************************
//    Main.java                   Nathan Gibson
//
//    A simple java gradebook to keep track of students and grades.
//*********************************************************************************

import java.util.Scanner;
public class Main 
{

  public static void main(String[]args)
  {
    Student group1 = new Student();
   
    Scanner sc = new Scanner(System.in);
    
    int selection;
    
    do{
      
      System.out.println("\tMenu");
      System.out.println("1.Add Students");
      System.out.println("2.Add Grades");
      System.out.println("3.Remove Students");
      System.out.println("4.Remove Grades");
      System.out.println("5.Exit");
    
      selection = sc.nextInt();
    
      if(selection == 1)
      {
        group1.addStudent();
        group1.printStudents();
      
      }
      if(selection == 2)
      {
        group1.removeStudent();
        group1.printStudents();
      }
      
      //more to come, just havent gotten to it.
      
    }while(selection != 5);
    
   
  }
}
    
    
      

import java.util.*;
public class Student
{
  public ArrayList students = new ArrayList();
  public String name;
  public Scanner sc = new Scanner(System.in);
  
  
  public void addStudent()
  {
    System.out.println("Please enter the student's name: ");
    String name = sc.nextLine();
    students.add(name);
       
  }
  
  public void removeStudent()
  {
    System.out.println("Please enter the student's name: ");
    String name = sc.nextLine();
    students.remove(name);
      
  }
  
  public void printStudents()
  {
    System.out.println(students); 
  }
  
}

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,802
  • Joined: 27-December 08

Re: Making a list of list?

Posted 22 August 2010 - 06:06 PM

First, not good design of the Student class. The Student class should model a single Student. No user input should be done in Student, nor should management of multiple Students. In general, user input should always be separate from abstractions like Student, Teacher, etc.

Also, with Java 1.5, Collections like ArrayList are generic. So you should be declaring them as ArrayList<Student>, ArrayList<Double>, etc., not ArrayList.
Was This Post Helpful? 1
  • +
  • -

#8 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: Making a list of list?

Posted 22 August 2010 - 06:25 PM

i cleared up the arraylist<> deal. apparently i was looking at an old tutorial online. thanks for the heads up. im aware that the design is really bad. im kind of doing it as a learning process though. once i get it to work the way i want, i can go through and make changes as i have time. kind of a long term project that i can add to as i figure things out. im really just trying to get it to work before i get too warped up in the layout of things.

anyways.. any ideas on why the method won't print like i intended?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,802
  • Joined: 27-December 08

Re: Making a list of list?

Posted 22 August 2010 - 06:37 PM

Worked for me when I ran your code.

Quote

im aware that the design is really bad. im kind of doing it as a learning process though. once i get it to work the way i want, i can go through and make changes as i have time.

Doing things ass-backwards is not a good design process. Do things correctly the first time and it will save you a huge headache down the road refactoring. Been there, done that. :)
Was This Post Helpful? 1
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Making a list of list?

Posted 22 August 2010 - 06:41 PM

In this case, the layout of things IS important to the functionality. The ArrayList won't print the way that you expect? You need to fix the class design for it to work the way you'd expect. Do something like this:

class Student {
    private List<Integer> grades = new ArrayList<Grades>();
    private String name;

    public Student(String n) {
        name = n;
    }

    public Student(String n, List<Integer> g) {
        name = n;
        grades.addAll(g);
    }

    public void addGrade(int g) {
        grades.add(g);
    }
 
    public String toString() {
        String s = name + ": ";
        for (Integer i: grades) 
            s += i.toString() + " ";
    }
}



There's a class that works. It keeps all of its information separate and allows you to manipulate that data only through the supplied methods, so nothing happens unexpectedly. The toString() method allows it to print itself when called by System.out.println(Student object);
Was This Post Helpful? 2
  • +
  • -

#11 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: Making a list of list?

Posted 22 August 2010 - 06:49 PM

View PostDogstopper, on 22 August 2010 - 05:41 PM, said:

In this case, the layout of things IS important to the functionality. The ArrayList won't print the way that you expect? You need to fix the class design for it to work the way you'd expect. Do something like this:

class Student {
    private List<Integer> grades = new ArrayList<Grades>();
    private String name;

    public Student(String n) {
        name = n;
    }

    public Student(String n, List<Integer> g) {
        name = n;
        grades.addAll(g);
    }

    public void addGrade(int g) {
        grades.add(g);
    }
 
    public String toString() {
        String s = name + ": ";
        for (Integer i: grades) 
            s += i.toString() + " ";
    }
}



There's a class that works. It keeps all of its information separate and allows you to manipulate that data only through the supplied methods, so nothing happens unexpectedly. The toString() method allows it to print itself when called by System.out.println(Student object);


thanks alot, i kind of let things get complex way too early. thanks for the simplifying insight. seeing it made it look alot more do-able.
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Making a list of list?

Posted 22 August 2010 - 06:56 PM

Sometimes it helps to program from the bottom up instead of from the top down. This means that one starts by looking at the Objects being used in the program starting at the farthest down (or abstract classes/interfaces) before thinking about how it's going to be used. In this example, Student is used in Main, but if you simply focus on Student and what it does, often you will get a better result. I use a mixture of these methods in all my programs.

Glad I could help.
Was This Post Helpful? 0
  • +
  • -

#13 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Making a list of list?

Posted 22 August 2010 - 07:27 PM

*edit*
Try saying something like....
"I messed up, this post is invalid"
instead. That way, your intentions aren't misleading.

~Dogstopper

This post has been edited by Dogstopper: 23 August 2010 - 03:35 AM

Was This Post Helpful? -3
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,802
  • Joined: 27-December 08

Re: Making a list of list?

Posted 22 August 2010 - 07:30 PM

No reason to post just to make someone feel like an idiot. -1.
Was This Post Helpful? 0
  • +
  • -

#15 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Making a list of list?

Posted 22 August 2010 - 07:32 PM

I posted a response however my response was not really a valid fix for what he asked. Since I cannot delete my post I removed the misleading content.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2