1

I'm working on some lab exercises for school. The following code is for a salary grade calculator. I'm seeing something weird in the console where the first time round, everything works, but if there's a second round of entering data, the part where a name has to be entered is skipped.

I've included the code, and a screenshot below. Can someone please help me?

import java.util.*;

public class readSalary {
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String name;
            int salary, meritPoints;
            String[] grade = {"A","B","C"};
            int terminator = 0;
            while (terminator == 0) {
                System.out.println("Enter Name \n");
                name = sc.nextLine();
                System.out.println("Enter Salary \n");
                salary = sc.nextInt();
                System.out.println("Enter Merit Points \n");
                meritPoints = sc.nextInt();

                if (salary > 600 && salary < 649) {
                    if (meritPoints < 10) {
                        System.out.println("Employee: "+name+" has a grade of: "+grade[2]);
                    }
                    else {
                        System.out.println("Employee: "+name+" has a grade of: "+grade[1]);
                    }
                }
                else if (salary > 700 && salary < 900) {
                    if (meritPoints < 20) {
                        System.out.println("Employee: "+name+" has a grade of: "+grade[1]);
                    }
                    else {
                        System.out.println("Employee: "+name+" has a grade of: "+grade[0]);
                    }
                }

                System.out.println("Type 1 to Quit, or 0 to Continue \n");
                terminator = sc.nextInt();
                if (terminator == 0) {
                    terminator = 0;
                }
                else {
                    terminator = 1;
                }

            }
            sc.close();
    }
}

The screenshot is below:

enter image description here

Thank You!

jerome
  • 329
  • 2
  • 5
  • 17

0 Answers0