Help with class assignment !

I'm writing a program that is used to convert binary input to octal and hexadecimal numbers. It is very basic program so I can only use loops, charAt, substrings, length.

Your program logic may process the input by taking groups of 3 digits for a single octal digit or 4 digits for a single hexadecimal digit.

Here is my code. Please help me finish it. It's 100 point project so I cannot miss it :(

Code :

// declare variable
String binaryNum, conversionType;
int i, j, k;
double result = 0;
String groupNum;
// create scanner object
Scanner keyboard = new Scanner(System.in);
// prompt user for input
System.out.print("Enter a binary number: ");
binaryNum = keyboard.next();
for (i = 0; i < binaryNum.length(); i++) {
if ((binaryNum.charAt(i) != '1' && (binaryNum.charAt(i) != '0'))) {
System.out.println("Error: Incorrect input. Binary numbers only use digits 0 and 1. "
+ "Reenter you input.");
System.out.print("Enter a binary number: ");
binaryNum = keyboard.next();
}// end of if
}// end of for loops
System.out.println("Enter the conversion type(Octal or Hexa): ");
conversionType = keyboard.next();
while (true) {
if (conversionType.equalsIgnoreCase("octal")) {
for (j = 0; j < binaryNum.length(); j += 3) {
groupNum = binaryNum.substring(j, j + 3);
for (k = 0; k < groupNum.length(); k++) {
result += groupNum.charAt(k) * Math.pow(2, k);
}//end of for loops
}// end of for loops
System.out.println(result);
System.exit(0);
}// end of if
}//end of while loops

Many thanks,

Re: Help with class assignment !

Please explain what the problem with the code is. Post the program's output and add some comments to it to explain what is wrong and show what the output should be.

Please edit your post and change the quote tags to code tags.

BTW the posted code won't compile because of missing parts.

Re: Help with class assignment !

I'm sorry because i'm not got at writing.

The problem is that I get stuck where I left right there.

I want to take group 3 numbers and converts into numerical number (from binary to octal). But I'm unable to figure out so there are no outputs at the end. Hopefully some one can help me get through this code.

Thanks,

Re: Help with class assignment !

Can you explain the steps the program must take to do the conversions?

Do them one at a time:

int to octal String

When you have to logic to do that, then do:

int to hexadecimal String

To start write down a sample input and the String you want to convert that to.

Quote:

process the input by taking groups of 3 digits for a single octal digit or 4 digits for a single hexadecimal digit.

Not sure I understand what that means. How can you get a single octal digit from 3 digits? Are you sure you copied that correctly?

Are the digits binary? Only "0" and "1"?

Would an example input would be the String: "110"

Re: Help with class assignment !

Let's say I have a string of number: 111101

I have to write a code that take first 3 numbers (which is 111) and convert it into octal. Then do the same thing for the next 3 (which is 101).

At the end, I will concatenate those two together to make a complete octal number.

Re: Help with class assignment !

You didn't post what the results will be for that input.

A solution would be to convert the binary String to an int and then convert that int to an octal String.

Work on one part at a time.

Re: Help with class assignment !

Oh it won't compile. It showed a message of error and terminate.

Re: Help with class assignment !

Quote:

Oh it won't compile. It showed a message of error and terminate.

Please copy the full text of the error message and paste it here.

1 Attachment(s)

Re: Help with class assignment !

Attachment 1446

here is after i fix it a little bit.

Re: Help with class assignment !

Please copy and paste here anything you need help with.

I can't read anything from that image.

Re: Help with class assignment !

lol sorry tho. It's so small.

Code :

if (conversionType.equalsIgnoreCase("octal")) {
for (j = 0; j < binaryNum.length(); j += 3) {
groupNum = binaryNum.substring(j, j + 3);
for (k = 0; k < groupNum.length(); k++) {
result += groupNum.charAt(k) * Math.pow(2, k);
}//end of for loops
}// end of for loops
System.out.println(result);
System.exit(0);

here is the result after compiling.

Code :

run:
Enter a binary number: 111
Enter the conversion type(Octal or Hexa):
octal
343.0
BUILD SUCCESSFUL (total time: 6 seconds)

Re: Help with class assignment !

You are confusing the value of a char with the value of an int. The char '1' does not have the save value as the int 1. To see then difference execute this:
Code :

System.out.println((int)'1' + " vs " + 1);

Can you explain what this statement is supposed to do:
Code :

groupNum.charAt(k) * Math.pow(2, k);

Re: Help with class assignment !

that statement is like the process that convert binary to octal

ex: 111 = 1 * 2^2 + 1 * 2^1 + 1*2^0

Re: Help with class assignment !

Try the code with "011" or "110" and consider the value of k.

Put the results in three columns, the digit, k and the value of the pow() method