0

I am now trying out different types of Java loops for my 5-year salary comparison program. So far, I seem to be having problems with trying out a do-while loop. I use NetBeans to build my programs and when I write the do-while loop in there, it produces several errors that I don't know how to resolve. Maybe my wording sounds incorrect, but let's check it out below to see what needs to be rewritten:

do {
System.out.println("Enter the salary individual 1 got in year 1: ");
    firstIndividualSalary[0] = scan.nextInt();

System.out.println("Enter the salary individual 2 got in year 1: ");
    secondIndividualSalary[0] = scan.nextInt();

if (firstIndividualSalary[0] == secondIndividualSalary[0]) {
        System.out.println("Error. Try again.");
        areAllDifferent = false;
        input.next();
    }

else {
  areAllDifferent = true;
}

while (!(areAllDifferent));

If a do-while loop isn't the right choice for making a user re-enter an answer until their input is correct, then please let me know if either while loops or for loops will make more sense in building a completely error-free program. I personally don't feel like a do-while loop might work at all. I just need to figure out how to make a user re-enter a couple of answers so they don't end up matching each other. And I need to figure out the same issue for calculating the total amounts of 2 individuals' 5-year salaries. I would appreciate it if you took a look at this and answered with some revision suggestions. Thanks.

GGomes4
  • 1
  • 1

1 Answers1

0

If a do-while loop isn't the right choice for making a user re-enter an answer until their input is correct, then please let me know if either while loops or for loops will make more sense in building a completely error-free program.

A do-while is completely appropriate for this purpose. In fact, a do-while loop guarantees that the code inside the do-while block is executed at least once which is desirable in such cases.

Structure you code as shown below:

do {
    areAllDifferent = true;
    System.out.println("Enter the salary individual 1 got in year 1: ");
    firstIndividualSalary[0] = Integer.parseInt(scan.nextLine());

    System.out.println("Enter the salary individual 2 got in year 1: ");
    secondIndividualSalary[0] = Integer.parseInt(scan.nextLine());

    if (firstIndividualSalary[0] == secondIndividualSalary[0]) {
        System.out.println("Error. Try again.");
        areAllDifferent = false;
    } 
} while (!areAllDifferent);

Note that I have used Integer.parseInt(scan.nextLine()) instead of scan.nextInt() to avoid issues with next(), nextInt() etc. as mentioned here.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
  • I am actually switching to while loops and so far, they produce zero errors. But thanks for your suggestions! – GGomes4 May 08 '20 at 22:45
  • @GGomes4 - If the answer helped you in any manner, please do not forget to accept the answer so that future visitors can also use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 11 '20 at 14:28