Problem with ArrayList

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1962 Views - Last Post: 26 March 2014 - 04:16 PM Rate Topic: -----

#16 Midi_   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 111
  • Joined: 22-May 12

Re: Problem with ArrayList

Posted 26 March 2014 - 03:09 PM

Look at the comments

        listStudent.add( new Student( s.getStudentFirstName(),s.getStudentLastName(), s.getStudentID(), s.getStudentGPA(), listCourse)); //Compiler does not recognize listCourse
        listCourse.clear(); //Compiler does not recognize listCourse


This post has been edited by Midi_: 26 March 2014 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

#17 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Problem with ArrayList

Posted 26 March 2014 - 03:16 PM

In your last code posting you commented out the listCourse array in the main method.

CasiOo has a nice solution posted on the previous page.
Was This Post Helpful? 1
  • +
  • -

#18 Midi_   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 111
  • Joined: 22-May 12

Re: Problem with ArrayList

Posted 26 March 2014 - 03:45 PM

View PostCasiOo, on 26 March 2014 - 02:49 PM, said:

A little code cleanup will make it easier to navigate and read through the code :)

...


Ok so I did everything you said, except for adding an "add course" method because it didn't need it. It ALMOST works perfectly. For some reason when I go to add a course it just prints out "Do you want to add a course?" the goes strait to the add a course part without waiting for a response. secondly, for some reason once I've added in all the info to add a course, it prints out both of the students and their courses. thoughts?

Thanks again

package myschool;

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

public class MySchool {


    public static void main(String[] args) {
        
        ArrayList<Student> listStudent = new ArrayList<>();
        boolean continueLoop = true;
        Scanner userInput = new Scanner(System.in);
        Scanner courseAddInput = new Scanner(System.in);
        int option;
        
        do{
        try {  
        System.out.println(" What would you like to do?");
        System.out.println(" 1) Add a student");
        System.out.println(" 2) View students");
        System.out.println(" 3) Remove a student");
        System.out.println(" 4) Exit");
        System.out.print("--> ");
        option = userInput.nextInt();
                
        switch( option ){
                case 1:
                    Scanner inputs = new Scanner(System.in);
                    String fName, lName;
                    int sID;
                    double sGPA;
                    
                    System.out.print(" First Name:");
                    fName = inputs.nextLine();
                    
                    System.out.print(" Last Name:");
                    lName = inputs.nextLine();
                    
                    System.out.print(" ID Number:");
                    sID = inputs.nextInt();
                    
                    System.out.print(" GPA:");
                    sGPA = inputs.nextDouble();
                    
                    Student newStudent = new Student(fName, lName, sID, sGPA);
                    listStudent.add(newStudent);

                    while (true) {
                        
                    System.out.println("Would you like to add a course? Y/N");
                    String shouldAddCourse = inputs.nextLine();

                    if( "N".equals(shouldAddCourse.toUpperCase()))
                               break;
                    
                    System.out.print(" CourseName:");
                    String cName = inputs.nextLine();

                    System.out.print(" Instructor:");
                    String instructor = inputs.nextLine();
                    
                    System.out.print(" CourseID:");
                    int cID = inputs.nextInt();

                    System.out.print(" CourseCredit:");
                    int cCred = inputs.nextInt();

                    Course newCourse = new Course(cName, instructor, cCred, "", "", cID);
                    newStudent.listCourse.add(newCourse);
                    
                    break;
                    }
                    
                case 2:
                    if(!listStudent.isEmpty()){
                        for(Student l:listStudent) {
                            System.out.println(l);
                            for(Course n:l.listCourse) {
                                System.out.println(n);
                            }
                            System.out.println();
                        }
                        
                        
                    }else
                    System.out.println("There are no students to view\n");
                    
                   break;
                    
                case 3:
                    Scanner removeChoice = new Scanner(System.in);
                    
                    try {
                        if(!listStudent.isEmpty()){
                        int j = 0;
                        System.out.println("Which student do you want to remove?");
                  
                            for(Student l:listStudent) {
                                System.out.print(j+1 + ")");
                                System.out.println(l);
                                j++;
                            }
                            int remove = removeChoice.nextInt();
                                listStudent.remove( remove - 1 );
                            System.out.println("Student has been removed\n");
                        }else
                            System.out.println("There are no students to remove\n");
                        
                    } catch (Exception e) {
                        System.out.println("There are no students to remove\n");
                    }
                    
                    break;
                    
                case 4:
                    continueLoop = false;
                    break;
        }
        } catch (Exception e) {
            System.out.println("That is not a valid option!!!");
            continueLoop = false;
        }
        
        }while( continueLoop );
    
    }
}



This is what happens:

run:
What would you like to do?
1) Add a student
2) View students
3) Remove a student
4) Exit
--> 1
First Name:Mike
Last Name:Smith
ID Number:12345
GPA:4
Would you like to add a course? Y/N
CourseName:Bio
Instructor:Reed
CourseID:4335
CourseCredit:4
FirstName:Mike LastName:Smith ID:12345 GPA:4.0
Course Name:Bio Course ID: Instructor:Reed Credit:4 Begin Time: End Time:

What would you like to do?
1) Add a student
2) View students
3) Remove a student
4) Exit
--> 1
First Name:Sarah
Last Name:Smith
ID Number:4534
GPA:4
Would you like to add a course? Y/N
CourseName:MAth
Instructor:long
CourseID:4354
CourseCredit:3
FirstName:Mike LastName:Smith ID:12345 GPA:4.0
Course Name:Bio Course ID: Instructor:Reed Credit:4 Begin Time: End Time:

FirstName:Sarah LastName:Smith ID:4534 GPA:4.0
Course Name:MAth Course ID: Instructor:long Credit:3 Begin Time: End Time:

What would you like to do?
1) Add a student
2) View students
3) Remove a student
4) Exit
--> 2
FirstName:Mike LastName:Smith ID:12345 GPA:4.0
Course Name:Bio Course ID: Instructor:Reed Credit:4 Begin Time: End Time:

FirstName:Sarah LastName:Smith ID:4534 GPA:4.0
Course Name:MAth Course ID: Instructor:long Credit:3 Begin Time: End Time:

What would you like to do?
1) Add a student
2) View students
3) Remove a student
4) Exit
-->

This post has been edited by andrewsw: 26 March 2014 - 03:51 PM
Reason for edit:: Reduced quote

Was This Post Helpful? 0
  • +
  • -

#19 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Problem with ArrayList

Posted 26 March 2014 - 03:48 PM

@Midi there is no need to keep quoting a large previous post, there is a Reply button further down the page, or you can edit/reduce the quoted text.

This post has been edited by andrewsw: 26 March 2014 - 03:49 PM

Was This Post Helpful? 1
  • +
  • -

#20 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Problem with ArrayList

Posted 26 March 2014 - 03:56 PM

Ah sorry
Calls to inputs.nextInt() and inputs.nextDouble() do NOT consume the line separator at the end
Which results in the next call to nextLine() will read the remaining line separator, because it wasn't consumed earlier, and return

Simply add a inputs.nextLine() before the while loop, just to consume the line separator from the previous input
Was This Post Helpful? 0
  • +
  • -

#21 Midi_   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 111
  • Joined: 22-May 12

Re: Problem with ArrayList

Posted 26 March 2014 - 04:16 PM

View PostCasiOo, on 26 March 2014 - 03:56 PM, said:

Ah sorry
Calls to inputs.nextInt() and inputs.nextDouble() do NOT consume the line separator at the end
Which results in the next call to nextLine() will read the remaining line separator, because it wasn't consumed earlier, and return

Simply add a inputs.nextLine() before the while loop, just to consume the line separator from the previous input


I can't believe it, its actually working, I thought this would never end haha. Thanks so much, looking at the code this way helps explain how the array list works with the object and how things are stored to it. I appreciate it. And thanks to you too Mike! you guys rule!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2