Search:

Type: Posts; User: aussiemcgr

Search: Search took 0.08 seconds.

  1. Replies
    20
    Views
    1,759

    Re: Rejecting certain inputs

    For your purposes, yes, because a loop is more simple and easier to understand (as well as a handful of other reasons around complexity).
    In my experience, recursion is most powerful when you are...
  2. Replies
    20
    Views
    1,759

    Re: Rejecting certain inputs

    A potential change to your current code (so numberEntered stays within the scope of the method):

    public static int studentNumberEntered(Scanner sc) {
    int numberEntered = 0;
    try { ...
  3. Replies
    20
    Views
    1,759

    Re: Rejecting certain inputs

    You are getting a stack overflow because of infinite recursion (a method calling itself forever with no exit-case). This would be occurring if your catch block is always being hit. My guess would be...
  4. Replies
    20
    Views
    1,759

    Re: Rejecting certain inputs

    You could do what Norm said, but a potentiality "better" solution (because it does not require the creation of exceptions) would be to do something like this:

    if(sc.hasNextInt()) {
    int...
  5. Replies
    20
    Views
    1,759

    Re: Rejecting certain inputs

    If you were using a GUI instead of the command prompt, there would be a handful of ways to do this.

    As for preventing the program from crashing, use and if statement and the Scanner's hasNextInt()...
Results 1 to 5 of 5