Skip to content

Updated OctalToDecimal.java #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions Conversions/OctalToDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}