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 5 of 5

Thread: Need help with an Octal Numbers program

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with an Octal Numbers program

    Hey, I have an Octal program I need help, I'll just copy and paste the info. Basically, my professor provides barely any help and there's restrictions to what type of code. For example, on this one, I can only use if/else statements. No arrays, no if/ands/or/nots, and no strings either.

    Here it is:

    For this program, you are going to convert decimal (integer) numbers into their octal number equivalents.

    The input to the program will be a non-negative integer number. If the number is less than or equal to 32767, convert the number to its octal equivalent. If the number is larger than 32767, output the phrase “Unable to Convert!”

    The output of your program will be a 5-digit octal number with no spaces between any of the digits. It may help to think that you may output each digit of the octal number individually as you calculate it, as long as you don't put spaces or new lines in your output between each digit.

    Here are a few examples of decimal numbers and their octal number equivalents:

    Decimal Number (the input from the user) Octal Equivalent (what your program would output)

    0 00000
    1 00001
    2 00002
    3 00003
    63 00077
    64 00100
    65 00101
    123 00173
    1000 01750
    10000 23420
    20000 47040
    32766 77776
    32767 77777

    Grading Notes

     Remember that your program requires a complete, digitally signed Honor Pledge to be graded.
     Name your class according to the “Class and File Name” section above.
     The octal number equivalent that you output should not have any spaces in it and should always be five digits.
     Your program should only process one number and then quit; do not process more than one number.
     Only use material through section 4.6 of the book, which includes the if and if/else structures. The if statement is the only necessary control structure, though the if/else could make your code a little more elegant.

    Two Separate Sample Program Runs (user input is underlined)

    Please enter a number between 0 and 32767 to convert: 2000
    Your integer number 2000 is 03720 in octal.


    Please enter a number between 0 and 32767 to convert: 50000
    Unable to Convert!

    And then here is what I have for code thus far:

    import java.util.Scanner;

    public class arowlands_Octal
    {
    public static void main ( String arg[] )
    {
    Scanner input = new Scanner ( System.in );

    int number;
    int octal;

    System.out.print ( "Please enter a number between 0 and 32767 to convert: ");

    number = input.nextInt();

    if (number <= 7)
    number1 = number

    if (number <= 63)
    number2 = number

    And there's where I got lost, I just feel like I'm approaching this all wrong and am totally off track.

    Thanks for any help.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Need help with an Octal Numbers program

    totally off track.
    I think you're on about the right track, if possibly at the other end of it.

    If we had to represent The Answer To Life, The Universe And Everything, we would certainly start by asking "how many tens is that?" and "after taking that many tens, how many units are left?". This is the same, except you're working with 5 octal number 'columns', so you'll be starting by asking "how many 8^4s is that?"

    the answer to life the universe and everything - Google Search

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

    Default Re: Need help with an Octal Numbers program

    Okay, so I've semi-figured it out. Here's what my code is now:


    public static void main ( String arg[] )
    {
    Scanner input = new Scanner ( System.in );

    int originalnumber;
    int number;
    int octal1;
    int octal2;
    int octal3;
    int octal4;
    int octal5;

    System.out.print ( "Please enter a number between 0 and 32767 to convert: ");

    number = input.nextInt();
    originalnumber = number;

    if (originalnumber > 32767)
    System.out.print ("Unable to Convert!");

    if (originalnumber < 0)
    System.out.print ("Unable to Convert!");

    octal1 = number % 8;
    number /= 8;

    octal2 = number % 8;
    number /= 8;

    octal3 = number % 8;
    number /= 8;

    octal4 = number % 8;
    number /= 8;

    octal5 = number %8;
    number /= 8;

    System.out.printf ("Your integer number " + originalnumber + " is %d%d%d%d%d in octal.\n", octal5, octal4, octal3, octal2, octal1);
    }
    }


    Everything is working except for when I enter a number less than zero or one larger than 32767, then it displays:

    Unable to Convert!Your integer number -2 is 0000-2 in octal.

    I'm just confused as to why it does both. Any help would be much appreciated!

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: Need help with an Octal Numbers program

    You did everything right except you forgot to return when the user enters a negative number.
    You can handle exceptional cases by throwing an exception. But for your simple example, you can just stop
    executing once you know the number is negative.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Need help with an Octal Numbers program

    You could also make what happens in your code a little more interdependent. At the moment you have two tests for invalid data that are independent of each other - the second validity test runs even when the first already rejects the data. Try using "if ... else if ... else" so that only one of ("Invalid", "Invalid", "Here's your number") is executed. You can execute more than one statement inside an if statement by enclosing them in 'curly brackets' like { /* statement */; /*statement*/ }

Similar Threads

  1. using Eclipse and every program gives the errorr with floating numbers
    By Neera in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2010, 12:11 PM
  2. Can't get seven random numbers, only one.
    By alpvii in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2010, 05:39 PM
  3. disticnt numbers
    By Fordy252 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 21st, 2010, 11:40 AM
  4. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  5. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM