From previous example we have shown how to convert hex to decimal in Java. This section we will be showing ways to convert octal to decimal in java. We would be using the same approach as previous example. The static method parseInt of Integer class is flexible to handle any base number. The base number we are talking about is the radix input on this method. Similar to our previous example that we have used the radix 16, on this case we will be using base 8 since we are talking about octal base number.

Octal to decimal conversion in Java

This sample source code shows how to convert octal to decimal in java. The approach would be to take the string input from the console, and then parse it using the static method of Integer class parseInt(String input, radix). Finally we will be surrounding our statements with a try catch block in order for us to determine if any invalid input that comes from the user console.

package com.javatutorialhq.tutorial;

import java.util.Scanner;

public class OctalToDecimalConverter {

	/*
	 * source code to covert octal to decimal in java
	 */

	public static void main(String[] args) {
		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{
			// 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
			System.out.println("Invalid Input, Expecting octal number 0-7");
		}
		finally{
			s.close();
		}
	}

}

Sample Output: java octal to decimal conversion

Octal Input:77
Decimal Equivalent : 63