77 *
88 */
99public class OctalToDecimal {
10-
10+
1111 /**
1212 * Main method
1313 *
14- * @param args Command line arguments
14+ * @param args
15+ * Command line arguments
1516 */
1617 public static void main (String args []) {
1718 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 );
2024 sc .close ();
2125 }
22-
26+
2327 /**
24- * This method converts an octal number to
25- * a decimal number.
28+ * This method converts an octal number to a decimal number.
2629 *
27- * @param o The octal number
30+ * @param inputOctal
31+ * The octal number
2832 * @return The decimal number
2933 */
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 {
3637 // 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:
4243 System .out .println ("Invalid Input, Expecting octal number 0-7" );
43- }
44- finally {
45- s .close ();
44+ return -1 ;
4645 }
4746 }
4847}
0 commit comments