2019-05-09 19:32:54 +08:00
|
|
|
package Conversions;
|
|
|
|
|
2017-04-21 02:56:21 +08:00
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts any Octal Number to a Decimal Number
|
|
|
|
*
|
2020-10-24 18:23:28 +08:00
|
|
|
* @author Zachary Jones
|
2017-04-21 02:56:21 +08:00
|
|
|
*/
|
|
|
|
public class OctalToDecimal {
|
2017-12-06 10:33:14 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* Main method
|
|
|
|
*
|
|
|
|
* @param args Command line arguments
|
|
|
|
*/
|
|
|
|
public static void main(String args[]) {
|
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
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
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
/**
|
|
|
|
* This method converts an octal number to a decimal number.
|
|
|
|
*
|
|
|
|
* @param inputOctal The octal number
|
|
|
|
* @return The decimal number
|
|
|
|
*/
|
|
|
|
public static int convertOctalToDecimal(String inputOctal) {
|
2017-12-06 10:33:14 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
try {
|
|
|
|
// Actual conversion of Octal to Decimal:
|
|
|
|
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");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|