-1

Possible Duplicate:
Decimal Conversion error

I am writing a program for a class and is having trouble figuring how to convert an octal number to an decimal number. Here is what I been trying to do so far:

   import java.util.Scanner;

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

    System.out.print("Enter number: ");
    int oct  = input.nextInt();
    int d2count = 0;
    int result=0;
    int d3count = 0;
    int d3 = 0;
    int d2 = 0;

    d3 = oct;
    d2 = oct;

    if(oct < 1000){
    while ( d3 >= 100){
    d3 = d3 - 100;
        d3count++;
    }}

    if (oct < 100){
    while ( d2 >= 10){
    d2 = d2 - 10;
        d2count++;
    }}

    result = (d3count * 64)+(d2count * 8) + d2;
System.out.printf("%d\n",result);
}
}

so basically I need a way to reduced a number to single digits (ie. 1337 into 1,3,3,7). I would really like to do it with what I have now, but my way of doing seems to have some errors that I can't see. It actually works if I enter a number less then 100, but When I enter a number higher then 100 the conversion is gets messed up somewhere. I am new to java so the more basic the techique the better, thanks

Community
  • 1
  • 1
user1714873
  • 135
  • 3
  • 3
  • 10
  • Do you really want to have result as an int? – hyde Oct 30 '12 at 20:42
  • You asked this question just a few hours ago too... http://stackoverflow.com/questions/13142977/decimal-conversion-error/13143884#13143884 Please don't start new threads for questions you have already posted – Joel Westberg Oct 30 '12 at 21:56

4 Answers4

4

The following converts from decimal to octal,

import java.util.Scanner;

public class test { 
    public static void main ( String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number: ");
        int oct  = input.nextInt();
        String result= Integer.toString(oct,8);
        System.out.println(result);
    }
}

The following converts from octal to decimal,

public static void main ( String args[]) {
     Scanner input = new Scanner(System.in);
     System.out.print("Enter number: ");
     String oct  = input.next();
     int result= Integer.parseInt(oct,8);
     System.out.println(result);
 }

The following is a more algorithmic way of converting from octal to decimal,

     public static int convert(String oct) {
         int i= 0;  
         for(int j = 0; j < oct.length(); j++) { 
                char num = oct.charAt(j);           
                num -= '0';     
                if(num<0||num>7) {          
                    sysout("invalid number");
                    return -1;
                }
                i *= 8;                          
                i += num;                      
            }
            return i;
        }
     }

The following is for converting a decimal to octal,

public static int convert(int OctalNumber){
   int counter=0;
   int result = 0;
   while(OctalNumber !=0) {
        int temp = (int) ((OctalNumber%8) * Math.pow(10, counter));
        counter++;
        result += temp;
        OctalNumber /= 8;
    }
    return result;
}
Arham
  • 2,072
  • 2
  • 18
  • 30
  • From the look of his code I think he is actually converting octal to decimal. – Jordan Kaye Oct 30 '12 at 20:36
  • So, he might be coding wrong because he said he wanted to convert decimal to octal. – Arham Oct 30 '12 at 20:39
  • More likely he mistyped his title since he said his code works for other input. Also, since this is for an assignment, I assume he will be looking for an actual algorithm. – Jordan Kaye Oct 30 '12 at 20:40
  • @Jordan Kaye updated my answer, take a look. – Arham Oct 30 '12 at 20:57
  • This is close.. you need to multiply by the proper power of 8 depending on your index. – Jordan Kaye Oct 30 '12 at 20:58
  • I'm picking up numbers from the left and not right so I don't need to care about the powers of 8. – Arham Oct 30 '12 at 21:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18822/discussion-between-jordan-kaye-and-arham) – Jordan Kaye Oct 30 '12 at 21:23
  • yea sorry I ment decimal to octal, and secondly sorry I don't think I can use that code simply because there are some thing I haven't learned yet in class, so we are forbidden to use them. – user1714873 Oct 31 '12 at 04:03
  • you cant use which part of code?? the algorithmic part? – Arham Oct 31 '12 at 04:10
  • Also, you offer a great way of doing it, however my instructer gave a template of the convert method, and it "requires" that i return the value as an int... – user1714873 Oct 31 '12 at 04:18
  • I probably could use the algorithmic part, however my comment aboves explains why I can not use it – user1714873 Oct 31 '12 at 04:19
  • here is the template `public static int convert(int OctalNumber)`{} – user1714873 Oct 31 '12 at 04:21
  • updated the answer. take a look. – Arham Oct 31 '12 at 04:39
  • doesn't seems to be right, I copy the code and paste it into my program, but no matter what number I enter the return is also a 0. – user1714873 Oct 31 '12 at 05:51
  • works for me though. If you are using an IDE, put breakpoints and try to debug. Before that try to understand what the program is doing rather than just copying. It'll help. – Arham Oct 31 '12 at 05:54
0

First of all, your code converts octal to decimal.

Secondly, you have

if(oct < 1000){

and

if (oct < 100){

but you have no if statement to handle cases in which the input number is greater than 1000.

Edit: I just realized that this really isn't the problem. When you start calculating d2 you need to use the end value of d3 after subtracting 100 however many times. So right before the second if you need a

d2 = d3;

You still need to handle inputs greater than 1000, though.

beaker
  • 16,331
  • 3
  • 32
  • 49
0

Use the shift functionality. Rather than using <1000 etc.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Tinman
  • 786
  • 6
  • 18
0

You could go with a bit of a better algorithm that allows any decimal number of reasonable length (ie. that will not cause an overflow):

public int octalToDecimal(int octal)
{         
   int numDigits = Integer.toString(octal).length();
   int decimalResult = 0;
   int mask = 0x00000007;
   for(int i = 0; i < numDigits; i++)
   {
      int octalDigit = octal & mask;
      octal = octal >> 3;    
      decimalResult += octalDigit * Math.pow(8, i);
   }

   return decimalResult;
}
Jordan Kaye
  • 2,837
  • 15
  • 15
  • I think your `mask` should be 7, not 0xF. Also, you should use powers of 10, not powers of 8. And you should shift `octal` by 3, not by 1. – hyde Oct 30 '12 at 21:12
  • Correct on the first and third accounts, but why would you multiply and octal digit using a power of 10? That would be the same as multiplying a binary digit with a power of 10 - makes no sense. – Jordan Kaye Oct 30 '12 at 21:22
  • Yeah, variable name `decimalResult` threw me off, I guess. My bad. – hyde Oct 30 '12 at 21:25