2

I wanted to make an input which accepts numbers from 1 - 10 and prints the range.

I need to check if the input is an integer (check), check if the range is 0-10 (check), and if it's not any of those things, to ask the user again. So, a recursive method?

Currently I have this:

import java.util.Scanner; import java.util.InputMismatchException; public class FinalTest { public static void main (String [] args) { Scanner in = new Scanner(System.in); int k = 0; System.out.print("int - "); try { k = in.nextInt(); } catch (InputMismatchException e) { System.out.println("ERR: Input"); System.exit(1); } if(k <= 10 && k > 0) { for(int j=1; j <= k; j++) { System.out.println(j); } } else { System.out.println("ERR: Oob"); System.exit(1); } } } 

I would like to replace the "System.exit()" so that it re attempts to ask the user for input again.

calling main(); produces an error.

How do I correctly call the main method in this case?

8
  • 4
    I'd suggest a loop. Commented Aug 4, 2017 at 11:16
  • 3
    You don't need recursion for this. Just use a loop. See: The while and do-while Statements Commented Aug 4, 2017 at 11:17
  • Stick it in a loop. Or alternatively put the logic into a function and call the function recursively. Commented Aug 4, 2017 at 11:17
  • 1
    For what it's worth you can use a recursive method for this but you shouldn't. A simple loop would suffice while(don't have valid answer){ask for answer} Commented Aug 4, 2017 at 11:18
  • Yes loop it: While (incorrect){} Commented Aug 4, 2017 at 11:18

4 Answers 4

1

Two choices here:

  • actually create a method and call that
  • simply use a loop

Loop could go like:

boolean askForInput = true; while ( askForInput ) { try { k = in.nextInt(); askForInput = false; } catch ... print "not a number try again" } 

But beyond that: you still want to put this code into its own method. Not because that code should call itself, but for clarity reasons. Like:

public static int askForNumber(Scanner in) { ... code from above return k; } 

And to answer your question: you do not want to use recursion here. You want to loop; and yes, recursion is one way to implement looping, but that is simply overkill given the requirement you want to implement here.

And for the record: when creating that helper method, you can actually simplify it to:

public static int askForNumber() { while ( askForInput ) { try ... return in.nextInt(); } catch ... print "not a number try again" } } 

Beyond that: you typically use recursion for computational tasks, such as computing a factorial, or fibonacci number, ... see here for example.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey, appreciate the answer. Very helpful. Just one thing - why isn't this working? boolean askForInput = true; while ( askForInput ) { System.out.print("int - "); try { k = in.nextInt(); askForInput = false; } catch (InputMismatchException e) { System.out.println("ERR: Input"); } }
It just keeps printing ERR. Shouldn't it prompt for input every time? Heres the pastebin url, for easier reading: pastebin.com/vPrXw3MX
@py9 I followed that link and put it into a simple main method ... it prints "int-" ... I enter a number, hit enter ... program ends. Thus: please put a minimal reproducible example there - something I can download as a whole and run to repro the problem.
the issue is when you dont input a integer, but a string. Shouldn't it go to the beginning of the loop and ask for an input again? (since the askForInput is still true). Here's the full code: pastebin.com/GAykyrwj
@py9 OK. I have to admit I simply had a wrong understanding how nextInt() works. So you might want to go with your previous try/catch code. I am honestly to busy today to start making experiments with that scanner myself.
1

for the part of the recursive method printing a range:

public void printAscending(int n) { if (n > 0) { printAscending(n - 1); System.out.println(n); } } 

Comments

1

I think using recursion is just too much for something that simple and would probably be more expensive. You can add a while loop around your scanning bit until the entered value is valid. I would also put the printing loop out of the while to not have to test a condition before printing since if you get out of the while loop, it means number if valid. You could test just the -1 value to exit process.

public class FinalTest { public static void main (String [] args) { Scanner in = new Scanner(System.in); int k = 0; do { System.out.print("int - "); try { k = in.nextInt(); } catch (InputMismatchException e) { System.out.println("ERR: Input"); System.exit(1); } } while(!(k>0 && k<=10) && k!=-1); if(k!=-1) { for(int j=1; j<=k; j++) { System.out.println(j); } } else { System.out.println("Bye Bye."); } } } 

Comments

1

Okay, so what I personally do when I need to use recursion is I create a separate function/method for it. And when I need to restart the method, I just call it within itself. So it would be something like this:

private void recursiveMethod() { // do stuff . . . if (yourCondition) { //continue to next piece of code } else { recursiveMethod(); } 

}

But in big projects, try to stay away from recursion because if you mess up, it can

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.