1

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();
    }
}
user4068770
  • 47
  • 2
  • 12

5 Answers5

2

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.

Arun Prakash
  • 1,717
  • 17
  • 26
0

Use

try
{
  // Code here 
}
catch (Exception e)
{
  // Do anything in case of error
}
  • Catching Exception is extremely bad practice. Please dont encourage it. – JB Nizet Oct 15 '14 at 06:31
  • "_Catching Exception is extremely bad practice_" , I don't really get it, could you please elaborate a bit ? @JBNizet – joey rohan Oct 15 '14 at 06:50
  • Catching exceptions i used to catch errors. If you can avoid it, try to write proper code and use Exceptions as an emergency case. And you shouldnt use the "Exception" since it includes all exceptions. Use specific exceptions like NullPointerExceptions, NumberFormatExceptions, ... – Emanuel Oct 15 '14 at 06:51
  • There are some cases where you can use catch to run additional code. It may be used for Filehandling (for example if a file cant be opened). In your case you shouldnt need it since you can verify using the scanner if the input is a number or a string. – Emanuel Oct 15 '14 at 06:58
  • 1
    @JBNizet Ahhh my bad, what you actually meant was catching Exception via Exception class :) – joey rohan Oct 15 '14 at 07:02
0

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
}
Siborgi
  • 11
  • 1
  • Catching Exception is extremely bad practice. Please dont encourage it. – JB Nizet Oct 15 '14 at 06:32
  • 1
    @JBNizet i dont think catching exception is a bad practice unless you know what kind of exception you are going to handle..i'm not saying it as a good practice to catch all the exceptions but it depends.. – Lucky Oct 15 '14 at 06:52
  • That's why I said "catching Exception", and not "catching exceptions". catch Exception catches everything, including an exception that you don't actually intend to catch. – JB Nizet Oct 15 '14 at 07:05
0

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. 
  }
}
Emanuel
  • 8,027
  • 2
  • 37
  • 56
0

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);
    }
}
Luong Dinh
  • 569
  • 4
  • 17