Hot to stop the program from crashing after a Stri

2019-08-10 22:10发布

问题:

How do I stop this from crashing when a String is entered instead of an int? Here is what i have. I've tried looking up some tutorials but I still couldn't figure it out. Thanks for the help guys. When a String is entered i need it to tell the user to enter an int

import java.util.Scanner;

    public class TaxCalc
    {
        public static void main(String [] args)
        {
          Scanner keyboard = new Scanner(System.in);

          int dependents = inputInt("Enter number of dependents: ", keyboard);

          int pigs = inputInt("Enter number of pigs: ", keyboard);

          double oinks= inputInt("Enter number of oinks: ", keyboard) -(pigs*500)+(200*dependents);

          System.out.println("Oinks after rewards: " + oinks);

          if(oinks<10000) oinks -= oinks*0.02; //2% tax
          else if(oinks<5000) oinks -= oinks*0.1; //10% tax
          else oinks -= oinks*0.2; //20% tax
          System.out.println("Oinks after penalties: " + oinks);
        }

    public static int inputInt(String prompt, Scanner keyboard){
        System.out.println(prompt);
        return keyboard.nextInt();
    }

    public double inputDouble(String prompt, Scanner keyboard){
        System.out.println(prompt);
        return keyboard.nextDouble();
    }
}

回答1:

By checking the InputMismatchException you can inform the users that they have entered an invalid input, and you can ask them to re-enter only Numbers.

public static int inputInt(String prompt, Scanner keyboard) {
        System.out.print(prompt);
        try{
            return keyboard.nextInt();
        } catch (InputMismatchException e){
            System.out.println("Input Mismatch! Please enter Numbers");
            keyboard.next();
            return inputInt(prompt, keyboard);
        }
}

Same for double also

hope you expected this one. if not please kindly comment your requiremet.



回答2:

Use

try
{
  // Code here 
}
catch (Exception e)
{
  // Do anything in case of error
}


回答3:

http://www.tutorialspoint.com/java/java_exceptions.htm

this website shows how to do exception handling on Java, basically do something like

try {
    int dependents = inputInt("Enter number of dependents: ", keyboard);
}
catch(Exception e) {
    //do something because an error occured
}


回答4:

Nonono, dont use exceptions (at least not Exception since there is a NumberFormatException) if you can avoid it.

If you want to be sure that only numbers are entered use

public double inputDouble(String prompt, Scanner keyboard){
  try {
   while (!keyboard.hasNextInt()) keyboard.next();
   return keyboard.nextInt();
  } catch (NumberFormatException e) {
    return 0; //not a numeric at all. 
  }
}


回答5:

I think, it work with you.

public static int inputInt(String prompt, Scanner keyboard){
    System.out.println(prompt);
    if(keyboard.hasNextInt())
            return keyboard.nextInt();
    else {
            System.out.println("please input a number");
            keyboard.next();
            return inputInt(prompt,keyboard);
    }
}


标签: java jgrasp