Home Java Tutorials Decimal to Octal Java Source Code Using toOctalString Method

Software Configurations

Follow Me

Decimal to Octal Java Source Code Using toOctalString Method

 

This java source code will show how to convert decimal number to octal number. To convert decimal to octal used toOctalString(Integer) method.

 

DecimaltoOctal.java

 

//copyrighted by topsourcecode.com
import javax.swing.JOptionPane;

public class DecimaltoOctal
{
 public static void main(String []a)
 {
    //Method 1
    String sNumber=JOptionPane.showInputDialog("Insert a Integer value");
    int number=0;
    try
    {
      number=Integer.parseInt(sNumber);
    }
    catch(Exception e)
    {
       JOptionPane one = new JOptionPane();
       JOptionPane.showMessageDialog(one,"Input value error(Expected a decimal value)");
       System.exit(-1);
    }
    
    String octal = Integer.toOctalString(number);
    JOptionPane one = new JOptionPane();
    JOptionPane.showMessageDialog(one,"Octal Value of Decimal "+number+" =\n" + octal);
 }
}

 

Result


Share this post