-1

Question 1: If an int greater or equal to 2 is entered by the user the first if statement becomes true and the code is executed setting the boolean error1 variable to false. The do loop should only repeat if the error1 variable is true according to my while statement. However the loop repeats regardless.

How can I make the first if statement when set to true exit the loop?

Question 2: I am using a try-catch code to help repeat the the do-while loop if anything other than an int is entered by the user. However, when something like abc or 12.3 is entered the first println of the try code is executed, the second line of the try statement asking for user input is ignored and the catch code is executed again. This becomes a non-terminating loop without user input.

How can I get the statement to ask for user input after the catch code is executed?

Here is my code:

import java.util.InputMismatchException;
import java.util.Scanner;

public class DeepbotCalc {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int points = 0;
    boolean error1 = false;

    do {
        try {
            System.out.println("How many points do you currently have?");
            points = input.nextInt();

            if (points >= 2){
                error1 = false;
            }

            else if (points > 0 && points < 2) {
                System.out.println("you need at least 2 points");
                error1 = true;
            }

            else if (points <= 0) {
                System.out.println("Please enter a positive whole number");
                error1 = true;
            }
        } catch (InputMismatchException e){
            System.out.println("Please enter a positive whole number.");
            error1 = true;
        }
    } while (error1 = true);
Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
Curse_USMC
  • 33
  • 7
  • 1
    Why did you edit your question with the solution? – Sotirios Delimanolis Jun 20 '16 at 22:00
  • Dido what @SotiriosDelimanolis said. How will future readers make sense of the answer/solution. – Michael Markidis Jun 20 '16 at 22:01
  • Never edit your question overriding the original code with your solution. If you want to add the solution, you can do it at the end as an update to the question. And remember to always format everything that is part of your code, not using apostrophes. – SpaceCore186 Jun 20 '16 at 22:05
  • I added the solution to my question so future readers won't answer the same part of the question multiple times. In the future I won't do this. I'll also use proper formatting when talking about my code. – Curse_USMC Jun 20 '16 at 22:30

2 Answers2

0

To stop the loop when the correct input is inserted,

while (error1 = true);

should become

while (error1 == true);

or even better

while(error1);

To fix the endless loop when the incorrect input is inserted, inside the catch add

input.nextLine();

to let the scanner "move on"

Loris Securo
  • 7,538
  • 2
  • 17
  • 28
0

This solves your problem:

import java.util.InputMismatchException;
import java.util.Scanner;

public class DeepbotCalc {

    public static void main(String[] args) {
        Scanner input;

        int points = 0;
        boolean error1 = false;

        do {

            try {
                input = new Scanner(System.in);
                System.out.println("How many points do you currently have?");
                points = input.nextInt();


                if (points >= 2) {
                    error1 = false;
                }

                else if (points > 0 && points < 2) {
                    System.out.println("you need at least 2 points");
                    error1 = true;
                }

                else if (points <= 0) {
                    System.out.println("Please enter a positive whole number");
                    error1 = true;
                }

            }

            catch (InputMismatchException e) {
                System.out.println("Please enter a positive whole number.");
                error1 = true;
            }

        } while (error1);
    }
}

I changed while(error1 = true) by while(error1), and added a new line of code in the try{} statement.

Each time the try{} statement is executed, a new Scanner object is created overriding the last one.

SpaceCore186
  • 586
  • 1
  • 8
  • 22