# Need help with an Octal Numbers program

• September 22nd, 2011, 03:54 PM
anrowl01
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

 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.
• September 22nd, 2011, 04:40 PM
Sean4u
Re: Need help with an Octal Numbers program
Quote:

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

• September 22nd, 2011, 10:42 PM
anrowl01
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!
• September 23rd, 2011, 12:04 AM
dabdi
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.
• September 23rd, 2011, 03:32 AM
Sean4u
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*/ }