Hello everyone,
I am doing an assignment that requires me to create an application that manages a student records database. They gave me the option to use a GUI or console application so I am making it a console app. The task requires that we use the abstract class pattern. The problem i am having is controlling flow when I call the add() method which is an abstract method in a super class named Student. I call the add() method from the starter class with the main method when I create a new Student object of type Undergraduate which is a class that extends the Student class. The add method in each subclass also contains a database connection with statements that are unique to that subclass. ie (Undergraduate, Graduate, Part-time). The specific problem that I am having is when using a Scanner to get the student information from the user the program skips whatever System out is last and falls straight down to the database connection. I know I have some kind of problem with my try catch. Here is what I got:
` public void add()

   {    
       try {
            Integer statusValue = 0;// for casting scanner values

            this.level = getLevel();   // call getLevel member method             
            setLevel(this.level);      // set local variable level

            studentID = getNewSID();  // call super class Student's "student ID generator" method
            setStudentID(super.studentID); //set the super class studentID instance field

            // Scanner for getting student information
            Scanner getStudentInfo = new Scanner(System.in);

            // Get student's first name
                System.out.println("Please enter student first name"); 
            firstName = getStudentInfo.nextLine();      

            setFirstName(super.firstName);  //set the super class first name instance field

            // Get student's last name
                System.out.println("Please enter student lastname");
            lastName = getStudentInfo.nextLine();

            setLastName(super.lastName);  // set the super class last name instance field

                // Get student's Residency status
                System.out.println("Please enter Student Status  (1 = Resident 2 = Non-resident)");
            statusValue = getStudentInfo.nextInt();
            if (statusValue.equals(1))
            {
               super.status = "Resident"; // if user choose 1
               setStatus(super.status);   // set super class status instance field
            } 
            else
            {
                super.status = "Resident";// if user choose 2
                setStatus(super.status);  // set super class status instance field
            }                

                // Get student GPA
                System.out.println("Please enter student GPA");
            gpa = getStudentInfo.nextDouble(); 

            setGpa(super.gpa);

                System.out.println("Please enter student mentor's name (20 char max) >");
            mentor = getStudentInfo.nextLine();

            setMentor(super.mentor);

    //////////////////////////////////need to control flow here//////////////////



      //////////////////////////////need to control flow here//////////////////


            Statement stmt;
            DBConnect conn = new DBConnect();
            stmt = conn.makeStatement();
            stmt.execute("Insert Into Student (studentID,firstName,lastName,status,gpa,mentor,level,thesisTitle,thesisAdvisor,company,type)"
                + " Values ('"
                + studentID + "', '"
                + firstName + "', '"
                + lastName + "', '"                    
                + status + "', '"
                + gpa + "', '"
                + mentor + "', '"
                + level + "', '"
                + "NA', '"
                + "NA', '"
                + "NA', '"
                + type + "')");

            stmt.close();
            conn.close();

        } catch (SQLException | InputMismatchException e) {
        System.err.println(e + "Could not insert");
     }

    System.out.println("Successfully added student to database");
    System.out.println("from Undergraduate class");

    }`

    Any help is appreciated very much.

Recommended Answers

All 2 Replies

There's a real problem with Scanner's design - so many people fall into this trap.
You have some input with an int followed by some text, eg
101
John Doe
... and you try to read it with
int num = scanner.nextInt();
String name = scanner.nextLine();
... and name is an empty String ("")!

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

Possible fixes:
1. Add add extra nextLine() between the nextInt and the real nextLine to clear the unwanted \n. This may be difficult if the nextInt and the nextLine are in different areas of code which are not always executed together.
2. Give up on nextInt (etc) and just read whole lines and parse then into ints (etc) with Integer.parseInt (etc) - in which case you can junk the whole scanner and use a BufferedReader instead.

Wow, Thanks James something so little as getStudentInfo.nextLine(); was all I needed. Thanks man I spent about five hours scratching my head on this. You mention the BufferReader. I don't know about that one yet. I will investigate it though. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.