1

So I am trying to write a method that checks if scanner input is an int, and loops errormessage until the user inputs an int. The method below works aslong as the usser doesn't give more than 1 wrong input. If I type muliple letters and then an int, the program will crash. I think it might have something to do with my try catch only catching 1 exception but not sure, and cant seem to get it to work. Does anyone know how I can fix this?

calling on method:

System.out.println("Write the street number of the sender: ");
int senderStreetNumber = checkInt(sc.nextLine);

method:

public static int checkInt (String value){
  Scanner sc = new Scanner(System.in);
  try{
    Integer.parseInt(value);
  } catch(NumberFormatException nfe) {
    System.out.println("ERROR! Please enter a number.");
    value = sc.nextLine();
    checkInt(value);
  }
  int convertedValue = Integer.parseInt(value);
  return convertedValue;
}
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
sodapunk
  • 47
  • 6
  • What is your reason for using recursion and ignoring the result of the nested calls? – Tom Nov 02 '17 at 15:42
  • I have to admit im pretty new to coding, and not sure how i would do that. – sodapunk Nov 02 '17 at 15:47
  • Possible duplicate of [What is a NumberFormatException and how can I fix it?](https://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – xenteros Nov 16 '17 at 17:04

3 Answers3

0

Your logic of recursive is not good.

Let me try to explain your error...

The first time you get in the function you "check if the value is a int) if not you to recurcive. Lets say the second time is good. then you cto the converted value then the recurvice kicks in and you come back to the first time you get in the fucntion. Then it does again the converted value and you don't catch that Exception so your application crash

DeadPool
  • 42
  • 1
  • 10
  • ahh, i see. yea i could tell im not using it correctly, thank you for your awnser. is there any way to use it correctly for what i need, or do you reccomend i use a different method altogether? – sodapunk Nov 02 '17 at 15:53
  • I would do a while loop or a do while till you get a good value and then return that value – DeadPool Nov 02 '17 at 15:54
  • Thank you for your help. I think I misunderstood what recursion is ment for, ill try to rewritethe method – sodapunk Nov 02 '17 at 15:57
0

Something like this. Did not code it in an IDE, just from brain to keyboard. Hope it helps. Patrick

Scanner sc = new Scanner(System.in);

int senderStreetNumber;
boolean ok = false;

while(!ok) {
    System.out.println("Write the street number of the sender: ");
    try {
        senderStreetNumber = Integer.parseInt(sc.nextLine());
        ok = true;
    } catch (NumberFormatException nfe) {
        System.out.println("ERROR! Please enter a number.");
    }
}
pat_b13
  • 151
  • 8
-1
This works.., just modified your program..tested

public static int checkInt(String value) {
        Scanner sc = new Scanner(System.in);
        try {
            return Integer.parseInt(value);
        }catch (Exception e) {
            System.out.println("Error please enter correct..");
            value = sc.nextLine();
            return checkInt(value);
        }
        //int convertedValue = Integer.parseInt(value);
        //return convertedValue;
    }