Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Convert Decimals to Octals using only If/Else

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    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);
            }
    }
    Last edited by jcattau; February 8th, 2011 at 10:31 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

                   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).
    // 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);

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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!");
            }
    }

Similar Threads

  1. Convert DOC,XLS to PDF with Java
    By comm in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 2nd, 2013, 05:10 AM
  2. [SOLVED] convert String to int
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: June 3rd, 2010, 04:05 AM
  3. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  4. Convert string to int?
    By connex in forum Java Theory & Questions
    Replies: 1
    Last Post: December 9th, 2009, 05:06 AM
  5. convert GSM to PCM (wav)
    By cilang in forum Java Theory & Questions
    Replies: 4
    Last Post: August 7th, 2009, 04:46 AM