-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivideByTwo.java
32 lines (31 loc) · 929 Bytes
/
DivideByTwo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Author:Ruizhi Pu
//import the package of the i/o sysytem
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
//this class is used to divide the int
public class DivideByTwo {
//main function
public static void main(String []args){
// the class bufferedread is used to deal with input stream
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int x;
String oneLine;
//output
System.out.println("Enter an Integer");
//try-catch is a function to catch the unexpected error
try{
//read the input by line
oneLine = in.readLine();
//put the input from string stream to int
x = Integer.parseInt(oneLine);
System.out.println("Half of x is " + ( x/2 ));
}
catch(IOException e ){//if the input is not a number
System.out.println( e );
}
catch(NumberFormatException e ){//if it is not a int
System.out.println( e );
}
}
}