-3

-Ive cut out the main code as it seems unnecessary, the issue seems with this, The code runs once then even if i type 'yes' as an answer to the scanner, the loop stops. It only loops once.

String bool = "yes";
    while (bool == "yes") {
        int e1 = whichExam();
        printinfo(pp, csn, isa, fwt, lds, afl, oop, e1);
        Scanner scanner = new Scanner(System.in);
        System.out.println("Do you want to check another exam ('yes' or 'no')?");
        bool = scanner.nextLine();
    }
Billies Wesley
  • 437
  • 1
  • 6
  • 21
  • 1
    It's hardly a duplicate, i didnt know what the issue was and if i knew i wouldn't have had to ask in the first place. If you judge duplicates like this a original question would be nigh on impossible. – Billies Wesley Apr 30 '17 at 17:17

1 Answers1

2

short answer:

you're comparing the strings incorrectly:

while (bool == "yes")

it should be:

while (bool.equals("yes"))

longer answer:

when you do while (bool == "yes") you're checking whether both String references point to the same object in memory and because that is not the case after the condition is checked the first time, hence the loop only executes once. you might also want to use equalsIgnoreCase(String other).

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126