Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: ArrayList

  1. #1
    Liquify is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default ArrayList

    Hey I'm struggling with Array Lists...
    Specification

    * Create a class Student with the following fields and methods:
    1. fName:String
    2. lName:String
    3. sNumber:Integer
    4. Major:String
    5. gpa:Double
    6. count:Integer (static)
    7. Default and paramaterized constructors
    8. set and get methods for each field
    9. Override toString()
    * Store individual student records in an ArrayList
    * Run the program from a menu with the following options:
    1. Add a student
    2. Find a student
    3. Delete a student
    4. Display all students
    5. Display the total number of students
    6. Exit
    So far I have
    import java.util.Scanner;
    import java.util.ArrayList;

    public class Student
    {
    private String fName;
    private String lName;
    private String Major;
    private double gpa;
    private static int count = 000000;

    private String counter;
    public Student(String f, String l, String m, double grade)
    {
    fName = f;
    lName = l;
    Major = m;
    gpa = grade;
    counter = "S00"+count++;

    }

    @Override
    public String toString()
    {
    String str = String.format("%s %s %s %s %.2f\n", counter, fName, lName, Major, gpa);
    for(Student s : students )
    {
    str+=String.format("\t%s\n", s.toString());
    }
    return str;
    }
    }

    import java.util.Scanner;
    import java.util.ArrayList;

    public class StudentTest
    {
    ArrayList<Student> students = new ArrayList<Student>();
    Scanner inputS = new Scanner(System.in);
    public static void main(String args[])
    {
    StudentTest st = new StudentTest();
    System.out.println(
    "Please select from the following menu:\n"
    +"\t1. Add a student\n"
    +"\t2. Find a student\n"
    +"\t3. Delete a student\n"
    +"\t4. Display all students\n"
    +"\t5. Display the total number of students\n"
    +"\t6. Exit\n");
    System.out.print("Your choice: ");
    Scanner input = new Scanner(System.in);

    int choice;

    do
    {
    choice = input.nextInt();

    switch (choice)
    {
    case 1: System.out.println("\nAdd a student\n");
    break;
    case 2: System.out.println("\nFind a student\n");
    break;
    case 3: System.out.println("\nDelete a student\n");
    break;
    case 4: System.out.println("\nDisplay all students\n");
    break;
    case 5: System.out.println("\nDisplay the total number of students\n");
    break;
    case 6: System.out.println("\nGoodbye!\n");
    break;
    default: System.out.println("\nYour choice," + choice
    + ", is invalid\n");
    break;
    }
    }while(choice != 0);
    }
    public void addStudent()
    {
    System.out.println("First Name:");
    String fName = inputS.nextLine();
    System.out.println("Last Name:");
    String lName = inputS.nextLine();
    System.out.println("Major Name:");
    String Major = inputS.nextLine();
    System.out.println("gpa:");
    double grade = inputS.nextDouble();
    students.add = new Student(fName, lName, Major, grade);
    }
    }
    I keep getting an error to adding a new student. I would appreciate it if someone could help me with this error so I can move on to the next part of the assignment.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    The syntax to add an element of type MyClass to an ArrayList<MyClass> called myList is:
    Java Code:
    [COLOR="Blue"]        myList.add(new MyClass(parameters));
    [/COLOR]
    not:
    Java Code:
    [COLOR="Red"]        myList.add = new MyClass(parameters);
    [/COLOR]
    -Gary-

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To the OP, you had several decent answers to your previous question thread here: Need help with java chp 4 Gross Pay

    but never responded to thank those who helped you or to update them on whether you solved the problem or not. I'm glad that this doesn't bother Gary and he gave you a great answer here regardless, but you may wish to be a little more considerate in the future.

    Luck.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    the class ListArray has a method called add() for adding objects to the list. when you want to add an student use

    students.add(new Student(fName, lName, Major, grade));

    thats it.

  5. #5
    Liquify is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    @Fubarable I'm sorry for not replying in the other post and thanking the other guys but thanks guys for the help I appreciate it.

  6. #6
    Liquify is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class Student
    {
    	private String fName;
    	private String lName;
    	private String Major;
    	private double gpa;
    	private static int count = 000000;
    	ArrayList<Student> students = new ArrayList<Student>();
    	private String counter;
    	public Student(String f, String l, String m, double grade)
    	{
    		fName = f;
    		lName = l;
    		Major = m;
    		gpa = grade;
    		counter = "S00"+count++;
    	
    	}
    
    	@Override
    	public String toString()
    	{
    		String str = String.format("%s %s %s %s %.2f\n", counter, fName, lName, Major, gpa);
          for(Student s : students )
          {  
             str+=String.format("\t%s\n", s.toString()); 
          }
          return str;
       } 
    }
    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class StudentTest
    {
    	ArrayList<Student> students = new ArrayList<Student>();
    	Scanner inputS = new Scanner(System.in);
    	public static void main(String args[])
    	{
    		StudentTest st = new StudentTest();
    		System.out.println(
    			"Please select from the following menu:\n"
    			+"\t1. Add a student\n"
    			+"\t2. Find a student\n"
    			+"\t3. Delete a student\n"
    			+"\t4. Display all students\n"
    			+"\t5. Display the total number of students\n"
    			+"\t6. Exit\n");
    		System.out.print("Your choice: ");
    		Scanner input = new Scanner(System.in);
    	
    		int choice;
    	
    		do
    		{
    			choice = input.nextInt();
    		
    		switch (choice)
    		{
    			case 1: System.out.println("\nAdd a student\n");
    				break;
    			case 2: System.out.println("\nFind a student\n");
    				break;
    			case 3: System.out.println("\nDelete a student\n");
    				break;
    			case 4: System.out.println("\nDisplay all students\n");
    				break;
    			case 5: System.out.println("\nDisplay the total number of students\n");
    				break;
    			case 6: System.out.println("\nGoodbye!\n");
    				break;
    			default: System.out.println("\nYour choice," + choice
    						+ ", is invalid\n");
    				break;
    		}
    		}while(choice != 0);		
    	}
    	 public void addStudent()
    	 {
    	 	System.out.println("First Name:");
    		String fName = inputS.nextLine();
    		System.out.println("Last Name:");
    		String lName = inputS.nextLine();
    		System.out.println("Major Name:");
    		String Major = inputS.nextLine();
    		System.out.println("gpa:");
    		double grade = inputS.nextDouble();
    		students.add(new Student(fName, lName, Major, grade));
    	 }
    
    }
    Okay so I'm stuck on the next part. I made 3 new sub-directories named StudentFind, StudentDelete, and StudentDisplay.
    so for StudentDisplay i would have to do something like
    Java Code:
          // display the Students in the list
          for ( int i = 0; i < students.size(); i++ )
             System.out.printf( " %s", students.get( i ) );
    and to show the size it have to be something like this
    Java Code:
    System.out.printf( "Size: %s\n", students.size() );
    For some reason it won't let me type in a user input for first name, last name, and ect.. after i choose a number from 1-6, for instance 1.to add a student. and I'm not quite sure how to call a method from one of the sub-directories after i choose a number if someone can help me out on this.
    Last edited by Liquify; 01-22-2011 at 07:19 AM.

  7. #7
    Liquify is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Question

    bump :( no reply from advisors

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    First off, you're missing some elements of your assignment. You have no default constructor, you don't have getters and setters for each field, and your toString() method doesn't make sense. You have an ArrayList<Student> called students in each Student object as well as in your StudentTest object -- is that really what you want? (It's always empty, so in a sense it doesn't really matter, but it doesn't belong there in the first place.)

    I don't understand what you mean by "3 new sub-directories" -- are you talking about new classes? You certainly don't need them. You have an addStudent() method that looks pretty good (you're going to run into some issues with your Scanner that just about everybody runs into when they're learning) but where do you invoke it? Where should you invoke it?

    The Scanner issue has to do with mixing nextInt() and nextDouble() calls with nextLine() calls, and clearing newline ('\n') characters from the input stream. It's subtle and confusing until you get the hang of it, and it was probably covered in your text or in your classroom discussion.

    -Gary-

  9. #9
    Liquify is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    @gary - okay ill start off from there. my instructor for some reason told me to make 3 new classes for StudentTest to call them. So your saying I can do all of that with just only Student and StudentTest? Also 1 more question, is there any topics that go into details that give examples of how to use the scanner input? also thx.

  10. #10
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

  11. #11
    Bake1tEazy is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    You never actually end up calling the method addStudent()

    Should look something like this

    case 1: System.out.println("\nAdd a student\n");
    st.addStudent();
    break;

  12. #12
    Liquify is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class StudentTest
    {
        public static void main(String args[])
        {
            ArrayList<Student> students = new ArrayList<Student>();
            int choice;
            
            printMenu();
            
            do
            {
                Scanner input = new Scanner(System.in);
                choice = input.nextInt();
           
            switch (choice)
            {
                case 1: System.out.println("\nAdd a student\n");
                	students.add(addStudent());
                    printMenu();
                    break;
                case 2: System.out.println("\nFind a student\n");
    					findStudent(students);
    					printMenu();
                    break;
                case 3: System.out.println("\nDelete a student\n");
    					displayAllStudents(students);
    					printMenu();
                    break;
                case 4: System.out.println("\nDisplay all students\n");
                	displayAllStudents(students);
                	printMenu();
                    break;
                case 5: System.out.println("\nDisplay the total number of students\n");
    					studentSize(students);
    					printMenu();
                    break;
                case 6: System.out.println("\nGoodbye!\n");
                    break;
                default: System.out.println("\nYour choice," + choice
                                                    + ", is invalid\n");
                    break;
            }
            }while(choice != 0);       
        }
        
        public static void printMenu(){
            System.out.println(
                    "\nPlease select from the following menu:\n"
                    +"\t1. Add a student\n"
                    +"\t2. Find a student\n"
                    +"\t3. Delete a student\n"
                    +"\t4. Display all students\n"
                    +"\t5. Display the total number of students\n"
                    +"\t6. Exit\n");
                System.out.print("Your choice: ");
        }
        
    	public static Student addStudent()
    	{
    		Scanner inputS = new Scanner(System.in);
    		System.out.println("First Name:");
    		String fName = inputS.nextLine();
    		System.out.println("Last Name:");
    		String lName = inputS.nextLine();
    		System.out.println("Major Name:");
    		String Major = inputS.nextLine();
    		System.out.println("Student Number:");
    		Integer sNumber = inputS.nextInt();
    		System.out.println("gpa:");
    		double grade = inputS.nextDouble();
    		return new Student(fName, lName, Major, sNumber, grade);
         }
    	
    	public static void displayAllStudents(ArrayList<Student> students) {
    		for (Student s: students) {
    			System.out.print(s.toString());
    		}
    	}
    	
    	public static Student findStudent(ArrayList<Student> students)
    	{
    		Scanner inputN = new Scanner(System.in);
    		String name = inputN.nextLine();
    		for(Student s: students)
    		{
    			if(s.getFName().equals(name))
    			{
    				System.out.println(s);
    			}
    			
    		}
    			return findStudent(students);		
    	}
    
    	public void deleteStudent(ArrayList<Student> students)
     	{
     		students.remove(findStudent(students));		
     	}
    	
    	public static void studentSize(ArrayList<Student> students)
    	{
    		System.out.printf("Size: %s\n", students.size());
    	}	
    }
    Java Code:
    public class Student
    {
        private String fName;
        private String lName;
        private String Major;
        private Integer sNumber;
        private Double gpa;
        private static Integer count = 0;
    
        public Student(){
        }
    
        public Student(String f, String l, String m, Integer sn, Double grade)
        {
            fName = f;
            lName = l;
            Major = m;
            sNumber = sn;
            gpa = grade;
            count++;
        }
    
        @Override
        public String toString()
        {
          return String.format("%s %s %s %d %.1f\n", fName, lName, Major, sNumber, gpa);
       }
    
    	public String getFName() {
    		return fName;
    	}
    
    	public void setFName(String name) {
    		fName = name;
    	}
    
    	public Double getGpa() {
    		return gpa;
    	}
    
    	public void setGpa(Double gpa) {
    		this.gpa = gpa;
    	}
    
    	public String getLName() {
    		return lName;
    	}
    
    	public void setLName(String name) {
    		lName = name;
    	}
    
    	public String getMajor() {
    		return Major;
    	}
    
    	public void setMajor(String major) {
    		Major = major;
    	}
    
    	public Integer getSNumber() {
    		return sNumber;
    	}
    
    	public void setSNumber(Integer number) {
    		sNumber = number;
    	}
    
    	public static Integer getCount() {
    		return count;
    	}
    
    	public static void setCount(Integer count) {
    		Student.count = count;
    	}
    }
    I keep getting an error for trying to delete a student. I'm almost done with my assignment but I still need help.

  13. #13
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    What is the error message?

    Best,
    --user0--

  14. #14
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Well, you need to know which Student the user wants to delete, right? I would suggest that displayAllStudents() needs to put a number before each Student's name, and then you'd prompt the user to enter a number, and use that to remove() the Student from the ArrayList.

    -Gary-

  15. #15
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Alternatively, you can just prompt for the Student's sNumber and use that. You'd need to loop through the ArrayList until you get a match.

    -Gary-

  16. #16
    Liquify is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    okay thanks for the help I'll try that out.

  17. #17
    Bake1tEazy is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    It seems as though after trying to find the student it won't go back to the menu.
    Also in your switch you need to change the exit of the loop to 6 rather than 0.
    while(choice != 6);
    Last edited by Bake1tEazy; 01-26-2011 at 04:34 AM.

  18. #18
    Bake1tEazy is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Did you end up finding out how to remove a student from the array list?

  19. #19
    Liquify is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class StudentTest
    {
        public static void main(String args[])
        {
            ArrayList<Student> students = new ArrayList<Student>();
            int choice;
            
            printMenu();
            
            do
            {
                Scanner input = new Scanner(System.in);
                choice = input.nextInt();
           
            switch (choice)
            {
                case 1: System.out.println("\nAdd a student\n");
                	students.add(addStudent());
                    printMenu();
                    break;
                case 2: System.out.println("\nFind a student\n");
    					findStudent(students);
    					printMenu();
                    break;
                case 3: System.out.println("\nDelete a student\n");
    					displayAllStudents(students);
    					deleteStudent(students);
    					printMenu();
                    break;
                case 4: System.out.println("\nDisplay all students\n");
                	displayAllStudents(students);
                	printMenu();
                    break;
                case 5: System.out.println("\nDisplay the total number of students\n");
    					studentSize(students);
    					printMenu();
                    break;
                case 6: System.out.println("\nGoodbye!\n");
                    break;
                default: System.out.println("\nYour choice," + choice
                                                    + ", is invalid\n");
                    break;
            }
            }while(choice != 6);       
        }
        
        public static void printMenu(){
            System.out.println(
                    "\nPlease select from the following menu:\n"
                    +"\t1. Add a student\n"
                    +"\t2. Find a student\n"
                    +"\t3. Delete a student\n"
                    +"\t4. Display all students\n"
                    +"\t5. Display the total number of students\n"
                    +"\t6. Exit\n");
                System.out.print("Your choice: ");
        }
        
    	public static Student addStudent()
    	{
    		Scanner inputS = new Scanner(System.in);
    		System.out.println("First Name:");
    		String fName = inputS.nextLine();
    		System.out.println("Last Name:");
    		String lName = inputS.nextLine();
    		System.out.println("Major Name:");
    		String Major = inputS.nextLine();
    		System.out.println("Student Number:");
    		Integer sNumber = inputS.nextInt();
    		System.out.println("gpa:");
    		double grade = inputS.nextDouble();
    		return new Student(fName, lName, Major, sNumber, grade);
         }
    	
    	public static void displayAllStudents(ArrayList<Student> students) {
    		for (Student s: students) {
    			System.out.print(s.toString());
    		}
    	}
    		
    	public static void findStudent(ArrayList<Student> students)
    	{
    		Scanner inputN = new Scanner(System.in);
    		String name = inputN.nextLine();
    		for(Student s: students)
    		{
    
    			if(s.getFName().equals(name))
    			{
    				System.out.println(s);
    				break;
    			}	
    			
    		}
    				
    	}
    
    	public static void deleteStudent(ArrayList<Student> students)
     	{
    		Scanner inputS = new Scanner(System.in);
    		String sNum = inputS.next();
     		for(Student s: students)
    		{
    			if(s.getSNumber().equals(sNum))
    			{
    				students.remove(sNum);
    				break;
    			}	
    		}				
     	}
    	
    	public static void studentSize(ArrayList<Student> students)
    	{
    		System.out.printf("Size: %s\n", students.size());
    	}	
    }
    I'm still having problems trying to delete a student, if someone can help me out on how to do this. I also fixed the problem to findStudent.

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Your goal is to remove a Student from the List, so why are you trying to remove a String?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  2. need help in ArrayList
    By wynects in forum New To Java
    Replies: 6
    Last Post: 11-19-2010, 09:32 AM
  3. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  4. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 03:34 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •