0

The desired loop I want is to have the program loop until the accepted condition is met. I have tried to switch around the operators to get the desired loop I want. I was certain that this was the correct code to meet my conditions but this causes my java console not to appear at all!

Can someone explain what I am doing wrong please?

while (total % 5 != 0) {


        System.out.println("Enter 3 a-z characters");

        Scanner scan = new Scanner(System.in);

        Pattern pattern = Pattern.compile("^[a-zA-Z]$");


        for (int i = 0; i < 3; i++) {
            String input = scan.nextLine();
            while (!pattern.matcher(input).matches()) {
                System.out.println("Incorrect");
                input = scan.nextLine();
            }
            int letter = input.charAt(0);
            if (letter >= 97 && letter <= 122) {
                total += letter - 96;
            } else if (letter >= 65 && letter <= 90) {
                total += letter + 36;
            }
        }

          if   (total % 5 == 0) {
              System.out.println("accepted");
          } else  {
              System.out.println("not accepted");

              scan.close();
          }
      }
  }
}

1 Answers1

1

As the initial value of total is 0, you never enter the while loop. Initialize total to a number that is not a multiple of 5. e.g. total=1.

This will only solve your problem of not seeing the input console.

To solve the problem I would use another variable to track if we can break the while loop.

public static void main(String[] args) {
    boolean accepted = false;
    Scanner scan = new Scanner(System.in);
    Pattern pattern = Pattern.compile("^[a-zA-Z]$");
    int total = 0;
    while (!accepted) {
        System.out.println("Enter 3 a-z characters");
        for (int i = 0; i < 3; i++) {
            String input = scan.next();
            while (!pattern.matcher(input).matches()) {
                System.out.println("Incorrect");
                input = scan.nextLine();
            }
            int letter = input.charAt(0);
            if (letter >= 97 && letter <= 122) {
                total += letter - 96;
            } else if (letter >= 65 && letter <= 90) {
                total += letter + 36;
            }
        }

        if (total % 5 == 0) {
            System.out.println("accepted");
            accepted = true;
            scan.close();
        } else {
            System.out.println("not accepted");
            total = 0;
        }
    }
}
Harihar Das
  • 454
  • 3
  • 11
  • But will that not affect the whole program itself , 3 inputs that total % 5 == 0 is the desired outcome? Will a value in total not disrupt user input? – Nexus_Valentine Dec 26 '19 at 19:01
  • @Nexus_Valentine Yeah, realistically you should just use a `do-while` loop when you want your loop to run at least a single time before checking the looping condition. You have a few other errors in the code too such as closing the `System.in` stream. – Nexevis Dec 26 '19 at 19:03
  • @Nexus_Valentine Yes, this does not solve the problem fully. It only solves your problem of `but this causes my java console not to appear at all`. – Harihar Das Dec 26 '19 at 19:08
  • Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at halfLife/halflife.HalfLife.main(HalfLife.java:19) – Nexus_Valentine Dec 26 '19 at 19:10
  • I get the above error when not accepted is reached. – Nexus_Valentine Dec 26 '19 at 19:11
  • @Nexus_Valentine I have updated the code to fix your other problem. – Harihar Das Dec 26 '19 at 19:18
  • @HariharDas Thank you, but this now happens.... a a a(3) =not accepted (loop), then I enter e, e, e (5) = not accepted (loop). Reset the program and enter e, e, e (5) = accepted . Ha , its very strange!! – Nexus_Valentine Dec 26 '19 at 19:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204903/discussion-between-nexus-valentine-and-harihar-das). – Nexus_Valentine Dec 26 '19 at 19:38