Convert Decimals to Octals using only If/Else

Hey guys, I need to convert decimal (integer) numbers to octal numbers. The range is only 0 to 32767 and I need to display all 5 numbers. So for example, decimal number 3 is octal number 00003, and decimal number 123 is octal number 00173.

I feel like I am really close but I keep getting an error about my "else if" statements and my "else" statement that says "'else' without 'if'". Can anyone help me out?

EDIT: Ok I figured out the compiler error, but I keep getting some funky numbers. Does anyone see my problem?

Code Java:

import java.util.Scanner;
public class jeremycattau_Octal
{
public static void main ( String args[] )
{
Scanner input = new Scanner (System.in);
int dig1 = 0;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
int dig5 = 0;
int octal, remain;
System.out.println("Please enter a number between 0 and 32767 to convert:");
octal = input.nextInt();
if (octal == 32767)
dig5 = octal / 4096;
remain = octal % 4096;
dig4 = remain / 512;
remain = remain % 512;
dig3 = remain / 64;
remain = remain % 64;
dig2 = remain / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
else if (octal >= 4096)
dig4 = octal / 512;
remain = remain % 512;
dig3 = remain / 64;
remain = remain % 64;
dig2 = remain / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
else if (octal >= 512)
dig3 = octal / 64;
remain = remain % 64;
dig2 = remain / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
else if (octal >= 64)
dig2 = octal / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
else if (octal >= 8)
dig1 = octal % 8;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
else System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
}
}

Re: Convert Decimals to Octals using only If/Else

There's no need for any if-else statements. They add to the clutter of the code and make it more difficult to debug. Calculate all 5 digits right away and just print them out.

You're running into problems because you didn't properly define what goes inside each if/else block. In Java, tabbings mean nothing. You must surround blocks with curly braces.

Code Java:

if (octal == 32767){
dig5 = octal / 4096;
remain = octal % 4096;
dig4 = remain / 512;
remain = remain % 512;
dig3 = remain / 64;
remain = remain % 64;
dig2 = remain / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
}

Otherwise, only the very first statement will be executed. Frankly, I don't see how you don't get compile errors with your current code (perhaps you didn't update your code here?)

As a side note, for something like this I would use bitwise operators (particularly shifting and bitwise-and).

Code Java:

// get the first two digits of a number
int number = 012;
int oct_d1 = number & 07; // oct_d1 = 2
int oct_d2 = (number & 070) >> 3; // oct_d2 = 1
// ... etc, can be repeated for any number of digits
System.out.printf("the number %d decimal is %d%d octal", number, oct_d1, oct_d2);

Re: Convert Decimals to Octals using only If/Else

I forgot to mention that this is for an introductory class and all we've worked up to so far is if/else statements, that's why I have to use them. So I have no idea what your talking about with bit operators haha.

This is my final code that works

Code Java:

import java.util.Scanner;
public class jeremycattau_Octal
{
public static void main ( String args[] )
{
Scanner input = new Scanner (System.in);
int dig1 = 0;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
int dig5 = 0;
int remain = 0;
int octal;
System.out.println("Please enter a number between 0 and 32767 to convert:");
octal = input.nextInt();
if (octal > 32767)
{
System.out.println("UNABLE TO CONVERT!");
}
else if (octal == 32767)
{
dig5 = octal / 4096;
remain = octal % 4096;
dig4 = remain / 512;
remain = remain % 512;
dig3 = remain / 64;
remain = remain % 64;
dig2 = remain / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
}
else if (octal >= 4096)
{
dig5 = octal / 4096;
remain = octal % 4096;
dig4 = remain / 512;
remain = remain % 512;
dig3 = remain / 64;
remain = remain % 64;
dig2 = remain / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
}
else if (octal >= 512)
{
dig4 = octal / 512;
remain = octal % 512;
dig3 = remain / 64;
remain = remain % 64;
dig2 = remain / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
}
else if (octal >= 64)
{
dig3 = octal / 64;
remain = octal % 64;
dig2 = remain / 8;
remain = remain % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
}
else if (octal >= 8)
{
dig2 = octal / 8;
remain = octal % 8;
dig1 = remain;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
}
else if (octal >= 0)
{
dig1 = octal % 8;
System.out.printf("Your integer number %d is %d%d%d%d%d in octal.\n", octal, dig5, dig4, dig3, dig2, dig1);
}
else System.out.println("UNABLE TO CONVERT!");
}
}