Octal to Decimal conversion: error message

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1430 Views - Last Post: 27 March 2013 - 05:49 AM Rate Topic: -----

#1 A_tothe_B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-March 13

Octal to Decimal conversion: error message

Posted 26 March 2013 - 02:20 PM

I am in a beginning java class and I'm working on a program that takes octal numbers and converts them to decimal. I am only allowed to use two methods: main () and convert (). When I compile this source code, I get the following error message:

method convert in class angelambrown_Decimal cannot be applied to given types;
System.out.printf ("%d:%d\n", octal, convert (octal));
^
required: int,int
found: int
reason: actual and formal argument lists differ in length
1 error

Could someone please shed some light on this??

 import java.util.Scanner; // program needs scanner to read input data

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

        int octal;
        int octalNumber;

            System.out.print ("Enter up to an 8-digit octal number and I will convert it for you: ");
        octal = input.nextInt();

        System.out.printf ("%d:%d\n", octal, convert (octal));

        while (octal >= 100000000 || octal < 0)
        {
            System.out.print ("Enter up to an 8-digit octal number and I will convert it for you: ");
            octal = input.nextInt();
        }
    }

    public static int convert (int octalNumber, int octal)
    {
        int pos_1;
        int pos_2;
        int pos_3;
        int pos_4;
        int pos_5;
        int pos_6;
        int pos_7;
        int pos_8;
        int value;

        if (octal >= 10)
        {
            pos_8 = octal / 10000000;
            value = octal % 10000000;
            pos_7 = value / 1000000;
            value = pos_7 % 1000000;
            pos_6 = value / 100000;
            value = pos_6 % 100000;
            pos_5 = value / 10000;
            value = pos_5 % 10000;
            pos_4 = value / 1000;
            value = pos_4 % 1000;
            pos_3 = value / 100;
            value = pos_3 % 100;
            pos_2 = value / 10;
            pos_1 = value % 10;

           octalNumber = pos_8 * 8^7 + pos_7 * 8^6 + pos_6 * 8^5 + pos_5 * 8^4 + pos_4 * 8^3 + pos_3 * 8^2 + pos_2 * 8^1 + pos_1;
        }
         return octalNumber;
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Octal to Decimal conversion: error message

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Octal to Decimal conversion: error message

Posted 26 March 2013 - 02:26 PM

Line 15 calls the method convert( int ) while the convert() method requires two int arguments, convert( int, int ).
Was This Post Helpful? 0
  • +
  • -

#3 A_tothe_B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-March 13

Re: Octal to Decimal conversion: error message

Posted 26 March 2013 - 02:57 PM

I tried using:

System.out.printf ("%d:%d\n", octal, convert (octal, octalNumber));

But it still won't work.. that was the first thing I tried. :(
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Octal to Decimal conversion: error message

Posted 26 March 2013 - 03:01 PM

Okay. Make that change, compile and run that result, if possible, and then post that code with any errors or questions you have about THAT code. "it won't work" is not something we can help you with.

This post has been edited by GregBrannon: 26 March 2013 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 A_tothe_B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-March 13

Re: Octal to Decimal conversion: error message

Posted 26 March 2013 - 04:41 PM

Thanks for your willingness to help. I changed it and compiled. Now my error message says that octalNumber might not have been initialized.

angelambrown_Decimal.java:33: error: variable octalNumber might not have been initialized
System.out.printf ("%d:%d\n", octal, convert (octal, octalNumber));
^
1 error


[codeimport java.util.Scanner; // program needs scanner to read input data

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

int octal;
int octalNumber;

System.out.print ("Enter up to an 8-digit octal number and I will convert it for you: ");
octal = input.nextInt();

System.out.printf ("%d:%d\n", octal, convert (octal, octalNumber));

while (octal >= 100000000 || octal < 0)
{
System.out.print ("Enter up to an 8-digit octal number and I will convert it for you: ");
octal = input.nextInt();
}
}

public static int convert (int octalNumber, int octal)
{
int pos_1;
int pos_2;
int pos_3;
int pos_4;
int pos_5;
int pos_6;
int pos_7;
int pos_8;
int value;

if (octal >= 10)
{
pos_8 = octal / 10000000;
value = octal % 10000000;
pos_7 = value / 1000000;
value = pos_7 % 1000000;
pos_6 = value / 100000;
value = pos_6 % 100000;
pos_5 = value / 10000;
value = pos_5 % 10000;
pos_4 = value / 1000;
value = pos_4 % 1000;
pos_3 = value / 100;
value = pos_3 % 100;
pos_2 = value / 10;
pos_1 = value % 10;

octalNumber = pos_8 * 8^7 + pos_7 * 8^6 + pos_6 * 8^5 + pos_5 * 8^4 + pos_4 * 8^3 + pos_3 * 8^2 + pos_2 * 8^1 + pos_1;
}
return octalNumber;
}
}[/code]

Sorry!!!! This will look better...


import java.util.Scanner; // program needs scanner to read input data

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

        int octal;
        int octalNumber;

        System.out.print ("Enter up to an 8-digit octal number and I will convert it for you: ");
        octal = input.nextInt();

        System.out.printf ("%d:%d\n", octal, convert (octal, octalNumber));

        while (octal >= 100000000 || octal < 0)
        {
            System.out.print ("Enter up to an 8-digit octal number and I will convert it for you: ");
            octal = input.nextInt();
        }
    }

    public static int convert (int octalNumber, int octal)
    {
        int pos_1;
        int pos_2;
        int pos_3;
        int pos_4;
        int pos_5;
        int pos_6;
        int pos_7;
        int pos_8;
        int value;

        if (octal >= 10)
        {
            pos_8 = octal / 10000000;
            value = octal % 10000000;
            pos_7 = value / 1000000;
            value = pos_7 % 1000000;
            pos_6 = value / 100000;
            value = pos_6 % 100000;
            pos_5 = value / 10000;
            value = pos_5 % 10000;
            pos_4 = value / 1000;
            value = pos_4 % 1000;
            pos_3 = value / 100;
            value = pos_3 % 100;
            pos_2 = value / 10;
            pos_1 = value % 10;

           octalNumber = pos_8 * 8^7 + pos_7 * 8^6 + pos_6 * 8^5 + pos_5 * 8^4 + pos_4 * 8^3 + pos_3 * 8^2 + pos_2 * 8^1 + pos_1;
        }
         return octalNumber;
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Octal to Decimal conversion: error message

Posted 26 March 2013 - 04:43 PM

Initialize octalNumber (line 10):

int octalNumber = 0;
Was This Post Helpful? 0
  • +
  • -

#7 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Octal to Decimal conversion: error message

Posted 26 March 2013 - 05:20 PM

Can I just interject for a second to point out something I noticed..?

The printf statement is setup to call...

convert(octal, octalNumber)


...but his method signature reads...

convert(octalNumber, octal)


Within the method scope, his variable octalNumber will have the value of octal, while the variable octal will have the value of octalNumber.

I'm not sure if that is going to have a significant effect on the program's output, but it shouldn't be left that way regardless. :)/>
Was This Post Helpful? 1
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,992
  • Joined: 20-September 08

Re: Octal to Decimal conversion: error message

Posted 26 March 2013 - 05:24 PM

Quote

System.out.print ("Enter up to an 8-digit octal number and I will convert it for you: ");
If they've entered an octal number, why are you dividing it by powers of ten?

'^' doesn't mean what you think it does. That's a bitwise XOR. You mean something more like Math.pow
Was This Post Helpful? 0
  • +
  • -

#9 A_tothe_B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-March 13

Re: Octal to Decimal conversion: error message

Posted 26 March 2013 - 06:31 PM

I am so ridiculously confused right now that it's not even funny. Thank you GregBrannon, My program will actually compile and run now. However, it's not producing a result. It prints the number I entered for octal and 0. So basically, it looks like this:

77:0

I'm guessing it has something to do with g00se's statement about my conversion being wrong. This is what a fellow student said he was doing so I just assumed he was right.. as far as using powers of 10 to convert. I don't know where to go from here. I understand how the conversion works when I do it on paper but I don't understand how to calculate it in java.
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Octal to Decimal conversion: error message

Posted 27 March 2013 - 03:54 AM

Stop being confused and just think it through logically, from start to finish. Confused is a temporary state, easily transitioned.

"I understand how the conversion works when I do it on paper but I don't understand how to calculate it in java."
Or paraphrasing, You can do it on paper, but you can't program it.

That's an important skill for a programmer to develop, but it comes with experience. You understand how the paper, pencil, and eraser all work together, and you have a number of mathematical tools at your fingertips, because you've been practicing, using them together and applying the math tools to many different kinds of problems for several years. Now you have to learn new tools, connect them logically and correctly, and get the danged machine to execute them for you.

Let's talk about the paper, pencil, and eraser approach:

Suppose you have a octal number, 13256. You've learned that each digit from right to left, or from the least significant to the most significant, is an increasing power of 8. Putting each of the octal in its place:
 8^4 | 8^3 | 8^2 | 8^1 | 8^0
  1     3     2     5     6
 

Then, converting to decimal is accomplished by calculating each value and summing them together:
 // starting with the LSB for a reason:
 decimalResult = 6 * 8^0 + 5 * 8^1 + 2 * 8^2 + 3 * 8^2 + 1 * 8^4
 

What are the steps? Roughly,
1. until there aren't any more, obtain the least significant digit (lsd) from the user's input
2. multiply the lsd by the appropriate power of 8
3. add the result of #2 to the decimal result
4. remove lsd from the user's input
5. repeat, starting with step 1.

How to program that? Well, each digit of the octal number can be stripped from the least significant end using the modulus operator. The octal number entered by the user is stored in Java as a decimal integer primitive. Your program is only "pretending" it's an octal number. Since it's really a decimal, you can use the modulus operator in a loop, treating the user's number like the decimal number it really is:
 Begin loop:
 // strip each least significant digit
 lsd = userOctal % 10;
 
 // multiply lsd by the appropriate power of 8 and add to decimal result
 
 // remove the lsd from the user's input
 userOctal /= 10;
 End loop.
 

It's your turn to fill in the rest. Hints: The convert method only needs to take one parameter and only requires two additional variables: the decimal result and a counter to determine the power of 8.

Keep the questions coming and keep coding!

Edit: Per gOOse's comment and to reduce confusion, I'm using '^' to represent "raising to the power of," but that's not how it's done in your program. For example:

8^4 is programmed as Math.pow( 8, 4 ).

Oh! And you should probably consider verifying that the user enters a valid octal number.

This post has been edited by GregBrannon: 27 March 2013 - 04:01 AM

Was This Post Helpful? 1
  • +
  • -

#11 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Octal to Decimal conversion: error message

Posted 27 March 2013 - 04:37 AM

I was intrigued by this assignment, since I've never used it before in any of my previous programming languages either.

So I took the time out last night before bed to work through it and I have a fully working robust example.

I will post it after you have a few more good tries.

post+="It was fun :)"

This post has been edited by Flukeshot: 27 March 2013 - 04:38 AM

Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,992
  • Joined: 20-September 08

Re: Octal to Decimal conversion: error message

Posted 27 March 2013 - 04:40 AM

Quote

The octal number entered by the user is stored in Java as a decimal integer primitive
No - it's stored as a String. I would, to avoid confusion and 'redundant processing' of decimals, treat the algo from that String and not convert it to a number first.
Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Octal to Decimal conversion: error message

Posted 27 March 2013 - 04:48 AM

View Postg00se, on 27 March 2013 - 07:40 AM, said:

Quote

The octal number entered by the user is stored in Java as a decimal integer primitive
No - it's stored as a String. I would, to avoid confusion and 'redundant processing' of decimals, treat the algo from that String and not convert it to a number first.

I was referring to the following statements, and I don't see why my statement is incorrect, but I'm standing by to learn (lines 12-13 of last posted code):
System.out.print ("Enter up to an 8-digit octal number and I will convert it for you: ");  
octal = input.nextInt();

Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,992
  • Joined: 20-September 08

Re: Octal to Decimal conversion: error message

Posted 27 March 2013 - 04:54 AM

Quote

and I don't see why my statement is incorrect
Well you're right in that particular context. What i'm getting at is that any input into a Java app via stdin starts out life as a String. It just so happens that nextInt is converting that input into an integral one. imo that conversion is not very useful and shouldn't be allowed to happen
Was This Post Helpful? 0
  • +
  • -

#15 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Octal to Decimal conversion: error message

Posted 27 March 2013 - 04:58 AM

So how do we work with numbers in String format?

I say this because my successful attempt to create a converter uses nextLine() to retrieve user info and then loops a substring() parsing into an int[] array.

To convert I simply multiply the values by a constant int[] array holding the values 1, 8, 64, 512 ... in the correct order.

Is this terribly inefficient?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2