Hot to stop the program from crashing after a String is entered instead of an int

白昼怎懂夜的黑 提交于 2020-01-05 08:10:23

问题


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);
    }
}


来源:https://stackoverflow.com/questions/26375630/hot-to-stop-the-program-from-crashing-after-a-string-is-entered-instead-of-an-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!