CarryDove
CarryDove

Reputation: 39

How do I repeat the last question when it's been answered wrong?

I know it sounds a little weird but I don't know how to word it. For example, I have an error message that shows up when the user inputs a number bigger than 2000000. However, after that it is supposed to repeat the "enter salary" question. If they put in a correct answer then the program asks if I would like to enter another employee. (Basically the loop restarts again). If they put in a wrong answer, again, the program repeats the same error message and is suppose to let the user input again until they give a valid answer.


import java.util.Scanner;
import java.text.*;
import java.io.*;

public class Project1 {

    public static void main(String[] args) throws FileNotFoundException

    {

        Scanner keyboard = new Scanner(System.in);
        Scanner user_input = new Scanner(System.in);
        Scanner scan = new Scanner(System.in);

        PrintWriter outFile = new PrintWriter("Project1.out");

        String employee_Fname;
        String employee_Lname;
        String employee_city;
        String employee_state;
        double empzip;
        String employee_job;
        double empsal;
        char again;
        int count = 1;
        String answer;

        do {

            System.out.print("Enter Employees First Name: ");
            employee_Fname = user_input.next();
            System.out.println();

            System.out.print("Enter the employee's last name: ");
            employee_Lname = user_input.next();
            System.out.println();

            System.out.print("Enter employee's city: ");
            employee_city = user_input.next();
            System.out.println();

            System.out.print("Enter employee's state: ");
            employee_state = user_input.next();
            employee_state.toUpperCase();
            System.out.println();

            System.out.print("Enter employee's zipcode: ");
            empzip = keyboard.nextDouble();
            System.out.println();

            System.out.print("Enter employee's job title: ");
            employee_job = user_input.next();
            System.out.println();

            System.out.print("Enter employee's salary: ");
            empsal = keyboard.nextDouble();
            System.out.println();

            if (empsal > 2000000) {
                System.out.println();
                System.out.println("Invalid salary entered! Please try again.");
                empsal = keyboard.nextDouble();
                System.out.println();

            } else

                System.out.print("Do you want to enter another employee? Y/N?");
            answer = keyboard.next();
        } while (answer.equals("Y"));

        outFile.printf("Employee first name is:  " + employee_Fname);
        outFile.printf("Employee last name is:  " + employee_Lname);
        outFile.printf("Employee city is:  " + employee_city);
        outFile.printf("Employee state is:  " + employee_state);
        outFile.printf("Employee zipcode is:  " + empzip);
        outFile.printf("Employee job is:  " + employee_job);
        outFile.printf("Employee salary is:  " + empsal);

        outFile.close();

    }
}

Does my question make sense?

Upvotes: 2

Views: 100

Answers (2)

CarryDove
CarryDove

Reputation: 39

hey guys thank you for helping but I think I figured it out. I basically just re typed the employee salary code into the while loop.

while(empsal > 2000000) {
            System.out.println();
            System.out.println("Invalid salary entered! Please try again.");

            System.out.print("Enter employee's salary: ");
            empsal = keyboard.nextDouble();
            System.out.println();



            System.out.println();
        }

Upvotes: 0

Smile
Smile

Reputation: 4088

I have made the changes in the code. Changes are between the comments // Changes start here and // Changes end here. I have added a while loop to keep checking on salary and ask user to enter it again if it is more than 2000000.


import java.util.Scanner;
import java.text.*;
import java.io.*;

public class Project1{

    public static void main(String[] args) throws FileNotFoundException

    {

        Scanner keyboard = new Scanner(System.in);
        Scanner user_input = new Scanner(System.in);
        Scanner scan = new Scanner(System.in);

        PrintWriter outFile = new PrintWriter("Project1.out");

        String employee_Fname;
        String employee_Lname;
        String employee_city;
        String employee_state;
        double empzip;
        String employee_job;
        double empsal;
        char again;
        int count = 1;
        String answer;

        do {

            System.out.print("Enter Employees First Name: ");
            employee_Fname = user_input.next();
            System.out.println();

            System.out.print("Enter the employee's last name: ");
            employee_Lname = user_input.next();
            System.out.println();

            System.out.print("Enter employee's city: ");
            employee_city = user_input.next();
            System.out.println();

            System.out.print("Enter employee's state: ");
            employee_state = user_input.next();
            employee_state.toUpperCase();
            System.out.println();

            System.out.print("Enter employee's zipcode: ");
            empzip = keyboard.nextDouble();
            System.out.println();

            System.out.print("Enter employee's job title: ");
            employee_job = user_input.next();
            System.out.println();

            System.out.print("Enter employee's salary: ");
            empsal = keyboard.nextDouble();
            System.out.println();

            // Changes start here
            while (empsal > 2000000) {
                System.out.println();
                System.out.println("Invalid salary entered! Please try again.");
                empsal = keyboard.nextDouble();
                System.out.println();
            }
            // Changes end here

            System.out.print("Do you want to enter another employee? Y/N?");
            answer = keyboard.next();

        } while (answer.equals("Y"));

        outFile.printf("Employee first name is:  " + employee_Fname);
        outFile.printf("Employee last name is:  " + employee_Lname);
        outFile.printf("Employee city is:  " + employee_city);
        outFile.printf("Employee state is:  " + employee_state);
        outFile.printf("Employee zipcode is:  " + empzip);
        outFile.printf("Employee job is:  " + employee_job);
        outFile.printf("Employee salary is:  " + empsal);

        outFile.close();

    }
}

Upvotes: 2

Related Questions