7
7
*
8
8
*/
9
9
public class OctalToDecimal {
10
-
10
+
11
11
/**
12
12
* Main method
13
13
*
14
- * @param args Command line arguments
14
+ * @param args
15
+ * Command line arguments
15
16
*/
16
17
public static void main (String args []) {
17
18
Scanner sc = new Scanner (System .in );
18
- int o = sc .nextInt ();
19
- System .out .println ("Decimal equivalent: " + convertOctalToDecimal (o ));
19
+ System .out .print ("Octal Input: " );
20
+ String inputOctal = sc .nextLine ();
21
+ int result = convertOctalToDecimal (inputOctal );
22
+ if (result != -1 )
23
+ System .out .println ("Result convertOctalToDecimal : " + result );
20
24
sc .close ();
21
25
}
22
-
26
+
23
27
/**
24
- * This method converts an octal number to
25
- * a decimal number.
28
+ * This method converts an octal number to a decimal number.
26
29
*
27
- * @param o The octal number
30
+ * @param inputOctal
31
+ * The octal number
28
32
* @return The decimal number
29
33
*/
30
- public static int convertOctalToDecimal (int o ) {
31
- System .out .print ("Octal Input: " );
32
- // Read the input from the console which we are expecting as an octal number:
33
- Scanner s = new Scanner (System .in );
34
- String inputHex = s .nextLine ();
35
- try {
34
+ public static int convertOctalToDecimal (String inputOctal ) {
35
+
36
+ try {
36
37
// Actual conversion of Octal to Decimal:
37
- Integer outputDecimal = Integer .parseInt (inputHex , 8 );
38
- System . out . println ( "Decimal Equivalent : " + outputDecimal ) ;
39
- }
40
- catch ( NumberFormatException ne ){
41
- // Printing a warning message if the input is not a valid octal number:
38
+ Integer outputDecimal = Integer .parseInt (inputOctal , 8 );
39
+ return outputDecimal ;
40
+ } catch ( NumberFormatException ne ) {
41
+ // Printing a warning message if the input is not a valid octal
42
+ // number:
42
43
System .out .println ("Invalid Input, Expecting octal number 0-7" );
43
- }
44
- finally {
45
- s .close ();
44
+ return -1 ;
46
45
}
47
46
}
48
47
}
0 commit comments