# How to code a repetitive respond in a Try/Catch?

**URL:** <https://forum.codewithmosh.com/t/how-to-code-a-repetitive-respond-in-a-try-catch/9934>\
**Category:** Java\
**Created:** [January 14, 2022, 12:05am UTC](https://forum.codewithmosh.com/t/how-to-code-a-repetitive-respond-in-a-try-catch/9934 "2022-01-14T00:05:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kernalson](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kernalson](https://forum.codewithmosh.com/u/kernalson)\
**Post date:** [January 14, 2022, 12:05am UTC](https://forum.codewithmosh.com/t/how-to-code-a-repetitive-respond-in-a-try-catch/9934/1 "2022-01-14T00:05:10Z")

</div>

New learner. I am learning try/catch today. It’s a simple code asking to enter a number and if it’s not a number, I want to ask the user another time to add a number. Can I add a code repeat asking user to add a number if they keep putting the non-number input with a try/catch? Thx!

```
public static void main (String[]args) {		
	Scanner keyBoardInput = new Scanner(System.in);
        System.out.println("enter a number: ");
	try { double num = keyBoardInput.nextDouble ();
	      System.out.println (num);			
	}
	catch (Exception e) {			  
	 System.out.println("Invalid,please enter a number:");
	}
```

---

<div class="post-metadata">

**Author:** ![eelsholz](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@eelsholz](https://forum.codewithmosh.com/u/eelsholz)\
**Post date:** [January 14, 2022, 1:41am UTC](https://forum.codewithmosh.com/t/how-to-code-a-repetitive-respond-in-a-try-catch/9934/2 "2022-01-14T01:41:17Z")

</div>

Yes you can. Have you learned about while loops? They repeat a section of code as long as a condition is true. For example in your code you could surround your try/catch in a while loop that will keep looping until the user has successfully entered a number.

```auto
public static void main(String[]args) { 
  Scanner keyBoardInput = new Scanner(System.in);
  System.out.println("enter a number: ");
  boolean success = false;
  while (!success) {
    try {
      double num = keyBoardInput.nextDouble ();
      System.out.println (num);
      success = true;
    }
    catch (Exception e) {		
      System.out.println("Invalid,please enter a number:");
      keyBoardInput.next ();
    }    
  }   
}

```

---

<div class="post-metadata">

**Author:** ![kernalson](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kernalson](https://forum.codewithmosh.com/u/kernalson)\
**Post date:** [January 14, 2022, 2:40am UTC](https://forum.codewithmosh.com/t/how-to-code-a-repetitive-respond-in-a-try-catch/9934/3 "2022-01-14T02:40:43Z")

</div>

Thank you! I just started to learn the loops, I have a question regarding the boolean success:  
I would assume the logics for ‘boolean success = false’ is that the user does not enter a number, that’s why it’s false? while !success in the while parenthesis already equals true, do I still need to illiterate ‘success = true’ in the try parenthesis?

---

<div class="post-metadata">

**Author:** ![eelsholz](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@eelsholz](https://forum.codewithmosh.com/u/eelsholz)\
**Post date:** [January 14, 2022, 11:46pm UTC](https://forum.codewithmosh.com/t/how-to-code-a-repetitive-respond-in-a-try-catch/9934/4 "2022-01-14T23:46:55Z")

</div>

You need to set `success = true` in the try to indicate that the user has successfully entered a number, otherwise you’ll be stuck in the loop forever. Once `success` is true, then `!success` will be false and you’ll break out of the `while (!success)` loop.

And there are other ways to do this same sort of thing, for example a do while loop might be more straightforward here.

```auto
public static void main(String[]args) { 
  Scanner keyBoardInput = new Scanner(System.in);
  System.out.println("enter a number: ");
  boolean isInvalid;
  do {
    isInvalid = false;
    try {
      double num = keyBoardInput.nextDouble ();
      System.out.println (num);
    }
    catch (Exception e) {
      isInvalid = true;
      System.out.println("Invalid,please enter a number:");
      keyBoardInput.next ();
    }    
  } while (isInvalid);  
}

```
