Octal To Decimal Java

Hey guys!! Welcome to flower brackets blog. Today we are going to learn octal to decimal java.

The conversion from octal to decimal takes place with the help of Integer wrapper class.

Also Read – Convert Decimal to Octal Java Program

This class provides a method Integer.parseInt(String s, int radix) which converts binary, hexadecimal and octal number to decimal number.

The parameters in the above method namely “String s” and “int radix” indicates

• String s – binary, hexadecimal and octal number,
• int radix – in what base the first argument is represented.

The method Integer.parseInt(String s, int radix) size is less than 32-bits. However, the size greater than 32-bits will throw an unchecked runtime java.lang.NumberFormatException.

Now let’s see an example on how to convert octal to decimal in java,

Example: Octal To Decimal Java

```import java.util.Scanner;

public class OctalToDecimal
{
int number;

void getValue()
{
System.out.println("Octal To Decimal Conversion In Java");
Scanner sc = new Scanner(System.in);
System.out.println("Please enter number for conversion: ");
number = Integer.parseInt(sc.nextLine(), 8);
}

void convertValue()
{
String decimal = Integer.toString(number);
System.out.println("Converted Decimal value is: " + decimal);
}

public static void main(String[] args)
{
OctalToDecimal otd = new OctalToDecimal();
otd.getValue();
otd.convertValue();
}
}

```

Output:

Octal To Decimal Java: using while loop

Let’s learn how to convert octal to decimal in java using while loop,

```import java.util.Scanner;

public class OctalToDecimalJava
{
public static void main(String[] args)
{
int octalNumber, decimalNumber = 0, a = 0, actual;
Scanner sc = new Scanner(System.in);

System.out.print("Please enter an octal number : ");
octalNumber = sc.nextInt();

actual = octalNumber;

while(octalNumber != 0)
{
decimalNumber = decimalNumber + (octalNumber % 10) * (int) Math.pow(8, a);
a++;
octalNumber = octalNumber / 10;
}

System.out.print("Java convert octal to decimal " + actual + " is :\n");
System.out.print(decimalNumber);
sc.close();
}
}

```

Output:

Please enter an octal number : 100
Java convert octal to decimal 100 is : 64

How to convert octal to decimal in java using Integer.parseInt() method

Now let’s see java convert octal to decimal using Integer.parseInt() method,

```public class OctalJava
{
public static void main(String[] args)
{
String strOctal = "131";
int decimalNumber = Integer.parseInt(strOctal, 8);
System.out.println("Octal to decimal java : " + decimalNumber);
}
}

```

Output:

Octal to decimal java : 89

How to convert octal to decimal using method

Here we are going to learn octal to decimal conversion using method. Below is octal code,

```public class OctalCode
{
public static int octalToDecimal(int octalNumber)
{
int decimalNumber = 0;
int a = 0;
while(true)
{
if(octalNumber == 0)
{
break;
}
else
{
int temp = octalNumber % 10;
decimalNumber += temp * Math.pow(8, a);
octalNumber = octalNumber / 10;
a++;
}
}
return decimalNumber;
}

public static void main(String[] args)
{
System.out.println("Converting from octal to decimal : " + octalToDecimal(131));
System.out.println("Converting from octal to decimal : " + octalToDecimal(32));
System.out.println("Converting from octal to decimal : " + octalToDecimal(100));
}
}

```

Output:

Converting from octal to decimal : 89
Converting from octal to decimal : 26
Converting from octal to decimal : 64

conclusion

That’s it guys. This post covered topics on converting from octal to decimal, how to convert octal to decimal in java and many more.

I hope you guys have understood the concept on octal to decimal conversion. You can subscribe to my blog flower brackets if you haven’t already.