Home > Back-end >  InputMismatchException execution
InputMismatchException execution

Time:03-12

Code that is to read a line 10 two columns of data, a line dividing a line dividend, and then output a new file including the divisor four columns, dividend, quotient and remainder, when the input file does not exist, the dividend is 0, the input for the decimal when all can run, FileNotFoundException ArithmeticException emerged and there is no problem, but when appear InputMismatchException output file is empty, Java small white, hope the great god help to look at, very grateful!
All the code below,

import java.util.Scanner;
Import the Java. Util. InputMismatchException;
Public class File {

Public static void main (String [] args) throws Java IO. FileNotFoundException {
Try {
The String encoding="GBK";
Java. IO. File the File=new Java IO. The File (" C: \ \ Users \ \ ldq2 \ \ Desktop \ \ 552 \ \ HW12 \ \ data. TXT ");
Scanner input=new Scanner ((file), encoding);
Java. IO. The File data=https://bbs.csdn.net/topics/new Java IO. The File (" C: \ \ Users \ \ ldq2 \ \ Desktop \ \ 552 \ \ HW12 \ \ output TXT ");
Java. IO. PrintWriter output=new Java IO. PrintWriter (data);
The output. The print (" dividend ");
The output. The print (" divisor ");
The output. The print (" quotient ");
The output. The println (" remainder ");
String D=input. Next ();
String d1=input. Next ();


While (input. HasNext ()) {
Try {

Int dividend=input. NextInt ();
Int divisor=input. NextInt ();
The output. The print (dividend + "");
The output. The print (divisor + "");
The output. The print (quotient (dividend and the divisor) + "");
The output. Println (remainder (dividend and the divisor) + "");
System. Out. Println (D + "" + dividend +" "+ +" "+ divisor d1);
System. Out.println (D + + "" +" "+ dividend d1 + +" quotient "+" "+ divisor
Quotient (dividend and the divisor) + "" +" remainder "+ remainder (dividend and the divisor) +" ");
}
The catch (InputMismatchException ex) {
The output. The println (" It must be a integer ");
Input. NextInt ();
continue;

}
The catch (ArithmeticException ex) {
Output. The println (" Divisor always be zero ");

}


}
Input. The close ();
The output. The close ();

}
The catch (Java. IO. FileNotFoundException e) {
System. Out.println (" File doesn 't the exists ");
System. The exit (0);
}


}
Public static int quotient (int number1, int number2) {
If (number2==0)
Throw new ArithmeticException (Divisor "always be zero");

Return number1/number2;
}

Public static int remainder (int number1, int number2) {
If (number2==0)
Throw new ArithmeticException (Divisor "always be zero");

Return number1 % number2;
}


}
  • Related