# octal to decimal conversion

Page 1 of 1

## 7 Replies - 32818 Views - Last Post: 09 December 2008 - 10:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=75918&amp;s=dc7c4a41b5b7ff9a2ce86ce61fec94bd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jess105_2

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 09-December 08

# octal to decimal conversion

Posted 09 December 2008 - 08:28 PM

in this program i need to convert octal to decimal. I can only use one main method and one method called convert (). So far I have been getting an error that says convert(int,int) in Program14 cannot be applied to (int)
System.out.printf ( "%d:%d\n", foo, convert (foo));
here is my code:

```import java.util.Scanner;

public class Program14
{
public static int main (String args [] )
{
int foo;
Scanner sc = new Scanner (System.in);
System.out.println ("Enter up to an 8-digit octal number and I'll convert it:");
foo = sc.nextInt ();
System.out.printf ( "%d:%d\n", foo, convert (foo));
int octalNumber;

while (foo >= 10000000) {
System.out.println ("Enter up to an 8-digit octal number and I'll convert it:");
}
}
public static int convert ( int octalNumber,int foo )
{
int dig8, dig7, dig6, dig5, dig4, dig3, dig2, dig1, remain;

if (foo >= 10) {
dig8 = foo / 10000000;
remain = foo % 1000000;
dig7 = remain / 1000000;
remain = dig7 % 1000000;
dig6 = remain / 100000;
remain = dig6 % 100000;
dig5 = remain / 10000;
remain = dig5 % 10000;
dig4 = remain / 1000;
remain = dig4 % 1000;
dig3 = remain / 100;
remain = dig3 % 100;
dig2 = remain / 10;
dig1 = remain % 10;

octalNumber = dig8 * 8^7 + dig7 * 8^6 + dig6 * 8^5 + dig5 * 8^4 + dig4 * 8^3 + dig3 * 8^2 + dig2 * 8^1 + dig1;

}
return octalNumber;

//			  }
}
}

```

can anybody help me?

*Welcome at DIC. Edited to add the [ code] tags Please

This post has been edited by pbl: 09 December 2008 - 09:19 PM

Is This A Good Question/Topic? 0

## Replies To: octal to decimal conversion

### #2 JBabineau

• D.I.C Head

Reputation: 6
• Posts: 68
• Joined: 05-December 08

## Re: octal to decimal conversion

Posted 09 December 2008 - 08:56 PM

your calling a method with one parameter, convert(foo)

but your convert() method has two parameters. When you look at your method you could add another parameter to your call or since your only using octalNumber as a return value do away with it in your method

Make convert() look like this:

public static void int convert(int foo)
{
}
Was This Post Helpful? 0

### #3 jess105_2

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 09-December 08

## Re: octal to decimal conversion

Posted 09 December 2008 - 09:21 PM

JBabineau, on 9 Dec, 2008 - 07:56 PM, said:

your calling a method with one parameter, convert(foo)

but your convert() method has two parameters. When you look at your method you could add another parameter to your call or since your only using octalNumber as a return value do away with it in your method

Make convert() look like this:

public static void int convert(int foo)
{
}

thanks so much!! i got rid of that error but now i am getting an error that says...
cannot find symbol
symbol : variable octalNumber
location: class Program14
return octalNumber;
^
1 error

i've been trying to play around with the parentheses to see if that helps but i have had no such luck

my code

import java.util.Scanner;

public class Program14
{
public static int main (String args [] )
{
int foo;
Scanner sc = new Scanner (System.in);
System.out.println ("Enter up to an 8-digit octal number and I'll convert it:");
foo = sc.nextInt ();
System.out.printf ( "%d:%d\n", foo, convert (foo));

while (foo >= 10000000) {
System.out.println ("Enter up to an 8-digit octal number and I'll convert it:");
}
}
public static int convert ( int foo )
{
int dig8, dig7, dig6, dig5, dig4, dig3, dig2, dig1, remain;

if (foo >= 10) {
dig8 = foo / 10000000;
remain = foo % 1000000;
dig7 = remain / 1000000;
remain = dig7 % 1000000;
dig6 = remain / 100000;
remain = dig6 % 100000;
dig5 = remain / 10000;
remain = dig5 % 10000;
dig4 = remain / 1000;
remain = dig4 % 1000;
dig3 = remain / 100;
remain = dig3 % 100;
dig2 = remain / 10;
dig1 = remain % 10;

int octalNumber = dig8 * 8^7 + dig7 * 8^6 + dig6 * 8^5 + dig5 * 8^4 + dig4 * 8^3 + dig3 * 8^2 + dig2 * 8^1 + dig1;

}
return octalNumber;

}
// }
}

is the return statement supposed to be included in the parentheses of the convert method or outside of it?
Was This Post Helpful? 0

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: octal to decimal conversion

Posted 09 December 2008 - 09:34 PM

Please use the [ code] tags
Facilate the reading of your code
Allow us to cut & paste your code into our Java IDE

Is this a scholl assigment ?
Are you allowed to use array ? Would be the code 10 times shorter
Was This Post Helpful? 0

### #5 jess105_2

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 09-December 08

## Re: octal to decimal conversion

Posted 09 December 2008 - 09:53 PM

```import java.util.Scanner;

public class Program14
{
public static int main (String args [] )
{
int foo;
Scanner sc = new Scanner (System.in);
System.out.println ("Enter up to an 8-digit octal number and I'll convert it:");
foo = sc.nextInt ();
System.out.printf ( "%d:%d\n", foo, convert (foo));

while (foo >= 10000000) {
System.out.println ("Enter up to an 8-digit octal number and I'll convert it:");
}
}
public static int convert ( int foo )
{
int dig8, dig7, dig6, dig5, dig4, dig3, dig2, dig1, remain;

if (foo >= 10) {
dig8 = foo / 10000000;
remain = foo % 1000000;
dig7 = remain / 1000000;
remain = dig7 % 1000000;
dig6 = remain / 100000;
remain = dig6 % 100000;
dig5 = remain / 10000;
remain = dig5 % 10000;
dig4 = remain / 1000;
remain = dig4 % 1000;
dig3 = remain / 100;
remain = dig3 % 100;
dig2 = remain / 10;
dig1 = remain % 10;

int octalNumber = dig8 * 8^7 + dig7 * 8^6 + dig6 * 8^5 + dig5 * 8^4 + dig4 * 8^3 + dig3 * 8^2 + dig2 * 8^1 + dig1;

}
return octalNumber;

}
// }
}
```

yes this is a school assignment but unfortunately we are not allowed to use arrays.
Was This Post Helpful? 0

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: octal to decimal conversion

Posted 09 December 2008 - 09:57 PM

jess105_2, on 9 Dec, 2008 - 08:53 PM, said:

yes this is a school assignment but unfortunately we are not allowed to use arrays.

OK give me 10 minutes
Was This Post Helpful? 0

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: octal to decimal conversion

Posted 09 December 2008 - 10:15 PM

```import java.util.Scanner;

public class OctalConversion {

// simple main method for static Java little job
public static void main(String[] arg) {
// Scanner to read user input
Scanner scan = new Scanner(System.in);
// prompt for String
System.out.print("Enter octal number: ");
// read it back
String octalStr = scan.nextLine();
// convert to int
int dec = convertToDecimal(octalStr);
// if not -1 conversion was OK
if(dec != -1)
System.out.println("Octal: " + octalStr + " is in decimal " + dec);
}

static int convertToDecimal(String octo) {
int number = 0;		 // init result
for(int i = 0; i < octo.length(); i++) {	// pass through all input characters
char digit = octo.charAt(i);			// fetch octal digit
digit -= '0';							// translate to number (integer)
if(digit < 0 || digit > 7) {			// validate user inpu
System.out.println("Your number is NOT a valid Octal number");
return -1;
}
number *= 8;							// shift to left what I already ahve
number += digit;						// add new number
}
return number;
}
}

```

I said 10 minutes... took me 15
But please in your future post
Was This Post Helpful? 0

### #8 jess105_2

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 09-December 08

## Re: octal to decimal conversion

Posted 09 December 2008 - 10:42 PM

thanks a lot for your help!!!
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }