My InputMismatchException isn't working, as when I put in something like a double into the input, instead of the exception output message, it gives me:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1.2"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at reviewGUI.NChooseR.main(NChooseR.java:32)
I've noticed a lot of other InputMismatchExceptions use scanners instead of DataInputStream, would I have to change it to that format in order for it to work? Here's part of my code:
public static void main(String[] args) throws IOException {
DataInputStream input = new DataInputStream (System.in);
int numChosenObj = -1;
int numTotalObj=-1;
try
{
System.out.print("Enter the number of objects to choose: ");
numChosenObj = Integer.parseInt(input.readLine());
}
catch(InputMismatchException e)
{
System.out.println("You must input an integer value.");
}
try
{
System.out.print("Enter the total number of objects to choose from: ");
numTotalObj = Integer.parseInt(input.readLine());
}
catch(InputMismatchException e)
{
System.out.println("You must input an integer value.");
}
int difference = numTotalObj-numChosenObj;
int rFact = factorial(numChosenObj);
int nFact = factorial(numTotalObj);
int differenceFact = factorial(numTotalObj-numChosenObj);
int totalWaysToChoose = nFact/(rFact*differenceFact);
System.out.println();
System.out.println("There are " + totalWaysToChoose + " ways to choose " + numChosenObj + " objects from " + numTotalObj + " total number of objects.");
}