JavaAlgorithms/Conversions/OctalToDecimal.java

47 lines
1.1 KiB
Java
Raw Normal View History

import java.util.Scanner;
/**
* Converts any Octal Number to a Decimal Number
*
* @author Zachary Jones
*
*/
public class OctalToDecimal {
2017-12-06 10:33:14 +08:00
/**
* Main method
*
2017-12-06 10:33:14 +08:00
* @param args
* Command line arguments
*/
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
2017-12-06 10:33:14 +08:00
System.out.print("Octal Input: ");
String inputOctal = sc.nextLine();
int result = convertOctalToDecimal(inputOctal);
if (result != -1)
System.out.println("Result convertOctalToDecimal : " + result);
sc.close();
}
2017-12-06 10:33:14 +08:00
/**
2017-12-06 10:33:14 +08:00
* This method converts an octal number to a decimal number.
*
2017-12-06 10:33:14 +08:00
* @param inputOctal
* The octal number
* @return The decimal number
*/
2017-12-06 10:33:14 +08:00
public static int convertOctalToDecimal(String inputOctal) {
try {
// Actual conversion of Octal to Decimal:
2017-12-06 10:33:14 +08:00
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");
2017-12-06 10:33:14 +08:00
return -1;
}
}
}