From 970be086b70dfded772fdc80f21294c60b8e0c96 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 6 Dec 2017 11:33:14 +0900 Subject: [PATCH] Updated OctalToDecimal.java --- Conversions/OctalToDecimal.java | 43 ++++++++++++++++----------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/Conversions/OctalToDecimal.java b/Conversions/OctalToDecimal.java index 637b8f2ec6dc..2c9098576fff 100644 --- a/Conversions/OctalToDecimal.java +++ b/Conversions/OctalToDecimal.java @@ -7,42 +7,41 @@ * */ public class OctalToDecimal { - + /** * Main method * - * @param args Command line arguments + * @param args + * Command line arguments */ public static void main(String args[]) { Scanner sc = new Scanner(System.in); - int o = sc.nextInt(); - System.out.println("Decimal equivalent: " + convertOctalToDecimal(o)); + System.out.print("Octal Input: "); + String inputOctal = sc.nextLine(); + int result = convertOctalToDecimal(inputOctal); + if (result != -1) + System.out.println("Result convertOctalToDecimal : " + result); sc.close(); } - + /** - * This method converts an octal number to - * a decimal number. + * This method converts an octal number to a decimal number. * - * @param o The octal number + * @param inputOctal + * The octal number * @return The decimal number */ - public static int convertOctalToDecimal(int o) { - System.out.print("Octal Input: "); - // Read the input from the console which we are expecting as an octal number: - Scanner s = new Scanner(System.in); - String inputHex = s.nextLine(); - try{ + public static int convertOctalToDecimal(String inputOctal) { + + try { // Actual conversion of Octal to Decimal: - Integer outputDecimal = Integer.parseInt(inputHex, 8); - System.out.println("Decimal Equivalent : " + outputDecimal); - } - catch(NumberFormatException ne){ - // Printing a warning message if the input is not a valid octal number: + Integer outputDecimal = Integer.parseInt(inputOctal, 8); + return outputDecimal; + } catch (NumberFormatException ne) { + // Printing a warning message if the input is not a valid octal + // number: System.out.println("Invalid Input, Expecting octal number 0-7"); - } - finally{ - s.close(); + return -1; } } } \ No newline at end of file