简体   繁体   中英

need some assistance with 2d arraylists

Class registration is an important aspect to college students. When registering for a course, you must know the course reference number (CRN).

The table below identifies some CRNs and their corresponding course titles.

CRN   | COURSE TITLE   
12451 | Introduction to IT Problem Solving Using Computer Programming  
15349 | Object-Oriented Techniques for IT Problem Solving   
18467 | Applied IT Programming  
16890 | Database Fundamentals  
13334 | Database Programming

I need to create a program to enroll students into these courses. A single enrollment into a course occurs when the user enters a CRN. Until the Registrar (user) has indicated they are finished entering CRNs, continue to prompt the user to enter a CRN. You must validate the CRN, providing an error message and re-prompting the user if an invalid CRN is entered. Keep track of the number of enrollments for each course. Course enrollments for a specific CRN cannot exceed its maximum enrollment, or else an error message will appear and the enrollment will not be processed.

Once the user has indicated they are finished entering CRNs, display a well-formatted report containing a list of each course with its CRN and title, the number of students enrolled and the number of seats remaining.

After running the code, 1. the quit doesn't do anything for a first couple of times 2. I still haven't figured out how to ignore overlapping inputs 3. I need to set the limits up to 30

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

class  TwoD
{

        private static boolean typeInt(String a) throws IOException {
  try {
     Integer.parseInt(a);
     return true;
    } catch (NumberFormatException e) {
     return false;
  }
    }

         public static void main(String[] args) throws IOException  
{
     {
         System.out.println("These are the courses that you can choose for the students to enroll. \n 12451 | Introduction to IT Problem Solving Using Computer Programming \n 15349 | Object-Oriented Techniques for IT Problem Solving \n 18467 | Applied IT Programming \n 16890 | Database Fundamentals  \n 13334 | Database Programming");

           ArrayList<String> name = new ArrayList<String>();
                Scanner scan = new Scanner(System.in);

        System.out.print("What is your student's name? ");      
    String student = scan.next();
    String a;

    while(!student.equalsIgnoreCase("quit")) {
        name.add(student); //after a cycle of while, ask user whether he or she would quit and print the arraylist of students.
        System.out.print("What is your student's name? ");
        student = scan.next();

    }

    ArrayList< ArrayList<String> > main = new ArrayList< ArrayList<String> >();
    for(int i = 0; i<name.size(); i++){
       ArrayList<String> course = new ArrayList<String>();
       for (int j = 0; j < name.size(); j++){

            System.out.print("The course the student is currently enrolling is : ");
        a = scan.next();

    while (!a.equalsIgnoreCase("quit"))
    {

  while(!typeInt(a) || Integer.parseInt(a) != 12451 && Integer.parseInt(a) != 15349 && Integer.parseInt(a) != 16890 && Integer.parseInt(a) != 13334 && Integer.parseInt(a) != 18467) {
            System.out.println("Check the courses again.");                     
         a = scan.next();
    }
        course.add(a);

        if (a.equalsIgnoreCase("quit")!=true)
        {
        System.out.print("The course the student is currently enrolling is : ");
        a = scan.next();
        }
        else break;
    }

       }
       main.add(course);
     }

    for(int i = 0; i < name.size(); i++){
        System.out.print("Name " + i + ": ");
        for(int j = 0; j < main.get(i).size(); j++){
            System.out.print(main.get(i).get(j) + " ");
        }
        System.out.println();
    }
}
}
}

Here's a link to a tutorial on how to debug. I used to spend hours on end trying to figure out what was wrong with my code and scrounging the internet for answers. Finally I had to learn when my final project wasn't working two hours before the deadline.

This skill will serve you well and save you hundreds of hours throughout your career.

Above mentioned link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM