0

Let me just start by saying I know that my code can be written more functionally, and I know that I have more variables than what I need. This is for a school assignment for an intro Java class, and I'm trying to follow instructions to the T.

import java.util.Scanner;

public class UniversityTest {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String personType;
        int userInput = 0;
        Person[] all_people = new Person[100];
        int count = 0;
        String name;
        String idNumber;
        float GPA;
        int admissionYear;
        int expGrad;
        int currYear;
        String position;
        int hireYear;
        float salary;
        String advisorName;
        String researchArea;
        int hours;
        float rate;

        while (count < 100){
            System.out.println("Please enter the category of person: ");
            personType = input.nextLine().toLowerCase();
            switch (personType){
            //if (personType.equals("student")){
            case "student":
                System.out.println("Enter the student's name: ");
                name = input.nextLine();
                System.out.println("Enter the student's ID number: ");
                idNumber = input.nextLine();
                System.out.println("Enter the student's GPA: ");
                GPA = Float.parseFloat(input.nextLine());
                System.out.println("Enter the student's admission year: ");
                admissionYear = Integer.parseInt(input.nextLine());
                System.out.println("Enter the student's expected year of graduation: ");
                expGrad = Integer.parseInt(input.nextLine());
                System.out.println("Enter the current year: ");
                currYear = Integer.parseInt(input.nextLine());
                Student new_student = new Student("Student", name, idNumber, GPA, admissionYear, 
                        expGrad, currYear);
                all_people[count] = new_student;
                count++;
                break;
            //} 
            //else if (personType.equals("grad student")){
            case "grad student":    
                System.out.println("Enter the grad student's name: ");
                name = input.nextLine();
                System.out.println("Enter the grad student's ID number: ");
                idNumber = input.nextLine();
                System.out.println("Enter the grad student's GPA: ");
                GPA = Float.parseFloat(input.nextLine());
                System.out.println("Enter the grad student's admission year: ");
                admissionYear = Integer.parseInt(input.nextLine());
                System.out.println("Enter the grad student's expected year of graduation: ");
                expGrad = Integer.parseInt(input.nextLine());
                System.out.println("Enter the grad student's position: ");
                position = input.nextLine();
                System.out.println("Enter the name of the grad student's advisor: ");
                advisorName = input.nextLine();
                System.out.println("Enter the grad student's area of research: ");
                researchArea = input.nextLine();
                System.out.println("Enter how many hours a week this grad student works: ");
                hours = Integer.parseInt(input.nextLine());
                System.out.println("Enter this grad student's hourly pay rate: ");
                rate = Float.parseFloat(input.nextLine());
                System.out.println("Enter the current year: ");
                currYear = Integer.parseInt(input.nextLine());
                GradStudent new_grad_student = new GradStudent("Grad Student", name, idNumber, GPA,
                        admissionYear, expGrad, currYear, position, advisorName, researchArea, hours, rate);
                all_people[count] = new_grad_student;
                count++;
                break;
            //}
            //else if (personType.equals("faculty")){
            case "faculty": 
                System.out.println("Enter the faculty member's name: ");
                name = input.nextLine();
                System.out.println("Enter the faculty member's ID number: ");
                idNumber = input.nextLine();
                System.out.println("Enter the faculty member's job: ");
                position = input.nextLine();
                System.out.println("Enter the year this faculty member was hired: ");
                hireYear = Integer.parseInt(input.nextLine());
                System.out.println("Enter this faculty member's annual salary: ");
                salary = Float.parseFloat(input.nextLine());
                Faculty new_faculty = new Faculty("Faculty", name, idNumber, position, hireYear, salary);
                all_people[count] = new_faculty;
                count++;
                break;
            //}
            //else {
            default:    
                System.out.println("Enter this person's name: ");
                name = input.nextLine();
                System.out.println("Enter this person's ID number: ");
                idNumber = input.nextLine();
                Person new_person = new Person(personType, name, idNumber);
                all_people[count] = new_person;
                count++;
                break;
            //}
            }//END OF SWITCH

            System.out.println("Press 1 to enter another user, or 0 to end.");
            userInput = input.nextInt();
            if (userInput == 0) {break;}
        }//END OF WHILE LOOP

        for(int i = 0; i < count; i++){
            System.out.println(all_people[i].toString());
        }
    }//END OF MAIN

 }

It works for the first run, but when it goes back to the top of the while loop it prints out the "Please enter the category of person: " but then jumps straight down to the default statement "Please enter the person's name: " without giving the user a chance to enter the type of person again. I originally did this with if statements (edited out) but tried switching it to a switch statement just in case that could have solved it. I have three other classes obviously but it would probably be a waste of time since I'm pretty sure the issues lies in this class.

Andy Turner
  • 122,430
  • 10
  • 138
  • 216
knokout1
  • 43
  • 7
  • The last line of your loop body, **after** your *`switch`*, is `userInput = input.nextInt();`. – Elliott Frisch Jul 10 '16 at 22:01
  • Can I suggest that you extract the contents of each of the cases into separate methods? This would make it both easier to read and easier to test. – Andy Turner Jul 10 '16 at 22:02
  • It being marked as a duplicate helped me find an answer. Thanks to all that helped and to the skilled mods that were able to direct me to an answer. This can be deleted and thank you again! – knokout1 Jul 10 '16 at 22:08

0 Answers0