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

Thread: Help with class assignment !

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
     // 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,
    Last edited by Ryoshiro; October 1st, 2012 at 04:25 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    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"
    Last edited by Norm; October 1st, 2012 at 04:40 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with class assignment !

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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with class assignment !

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with class assignment !

    f39c7790168c79aff5a0fc4d2c9d747d_49670266.java.jpg
    here is after i fix it a little bit.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with class assignment !

    Please copy and paste here anything you need help with.
    I can't read anything from that image.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with class assignment !

    lol sorry tho. It's so small.
    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.
    run:
    Enter a binary number: 111
    Enter the conversion type(Octal or Hexa): 
    octal
    343.0
    BUILD SUCCESSFUL (total time: 6 seconds)

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    System.out.println((int)'1' + " vs  " + 1);
    Can you explain what this statement is supposed to do:
    groupNum.charAt(k) * Math.pow(2, k);
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  2. Instrument Class Assignment Help
    By kinney.j in forum Object Oriented Programming
    Replies: 1
    Last Post: November 2nd, 2011, 07:27 AM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  4. 1st Java class assignment, and having some difficulties. Little help please.
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 27th, 2011, 04:09 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM