1

I am just trying to get code to work where the code asks again for an answer, if text or a symbol is entered, instead of a required integer:

import java.util.Scanner;

class timecalc {

int hrs = 0;
int min = 0;
static int hourflag = 0;
static int minflag = 0;
Scanner sc = new Scanner(System.in);

public int getHours() {

    try {
        hourflag = hourflag + 1;
        if (hourflag > 1) {
            System.out.println("Invalid month Please enter hours again:");
        }
        System.out.println("Enter month:");
        return hrs = sc.nextInt();

    } catch (InputMisMAtchException e) {
        System.out.println("entered invalid input " + e);
    }
}

Have reviewed answers already given but cant get a workable solution

Any ideas?

AFanmhi
  • 33
  • 6

1 Answers1

1

I won't give you the entire code, but just a hint or psuedo-code. As an exercise you can implement it as per your requirement.

System.out.println("Enter month:");
while (true) {
    try {
        int min = sc.nextInt();
        break;
    } catch (InputMismatchException ex) {
        System.err.println("Invalid input, please enter again");
        sc.nextLine();  //  <----- advance the scanner
    }
}

Here the logic is to loop until we get the right input. If it is an invalid input, the loop never breaks.

Also as a side-note, I would recommend you to create just one method to fetch correct inputs and call it respectively from other methods. Rather than duplicating this logic everywhere.

Nicholas K
  • 14,118
  • 7
  • 25
  • 49
  • Hi, thanks a mil for this, just I know it aint a matter of copying or pasting but working away. – AFanmhi Nov 29 '18 at 18:53
  • Hi, Im not getting this to work. I do get the idea of a loop to work until a number is added but still cant get it applied to the code. Thanks again. – AFanmhi Nov 29 '18 at 20:27
  • What is the issue you are facing? – Nicholas K Nov 30 '18 at 03:57
  • You have to be more specific. What exception are you facing? What isn't working? – Nicholas K Nov 30 '18 at 09:41
  • { try {monthflag = monthflag + 1 ; if(monthflag > 1){ System.out.println("Invalid month Please enter month again:"); } System.out.println("Enter month:"); return month =sc.nextInt(); while (true) { try { int min = sc.nextInt(); break; } catch (InputMismatchException ex) { System.err.println("Invalid input, please enter again"); sc.nextLine(); } } – AFanmhi Nov 30 '18 at 10:15
  • Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) – AFanmhi Nov 30 '18 at 10:16
  • Thats error message now. In code, have first loop to ask for correct month (>1 and <12), but second loop you suggest to loop back until answered isnt working. Thats the problem. – AFanmhi Nov 30 '18 at 10:17
  • That is caused due to another issue. Read [this](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) to solve it. – Nicholas K Nov 30 '18 at 10:24
  • Basically its an issue while advancing the scanner obj, that link will help you resolve it. – Nicholas K Nov 30 '18 at 10:36
  • I was just thinking, would a simple "if else" <> a number work too, just "if hrs <> 1 to 12, to ask again? – AFanmhi Nov 30 '18 at 12:39
  • It's hard to understand what exactly you've tried by what you've pasted in the comments. – Nicholas K Nov 30 '18 at 13:25
  • Was going to add an extra if statement like, if (monthflag > 1) { System.out.println("Invalid month Please enter month again:"); } – AFanmhi Nov 30 '18 at 14:53