-1

So I am trying to change an octal to a regular base 10 number, but I can't understand where my code is supposed to go from here. What I tried to do was change the int into a string and then use the individual characters to create the base 10 numbers for each place, but it isn't working.

import java.util.Scanner;
import java.lang.Math; 

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

  System.out.println("Enter octal with less than 9 digits");
  int octal = s.nextInt();
  String ocStr = Integer.toString(octal);

  int ocLen = ocStr.length();
  int flag = 0;
  int baseTen = 0;
  char temp;
  int power = ocLen - 1;
  Double tempDub;

  if (ocStr.contains("8") || ocStr.contains("9") || ocStr.length()>8){
    flag++;
    System.out.println("ERROR: Incorrect Octal Format");
  }
  if (flag<1){
    for (int i = ocLen-1; i >0; i--){
      temp = ocStr.charAt(i);
      System.out.println(temp);
      tempDub = 1.0*Character.getNumericValue(temp);
      System.out.println(tempDub);
      tempDub = java.lang.Math.pow(tempDub,power);
      System.out.println(tempDub);
      int tempInt = Integer.valueOf(tempDub.intValue());
      System.out.println("Changed Character is: " + tempDub);
      baseTen = baseTen + tempInt;
      System.out.println("New answer so far is: " + baseTen);
      power--;
    }
    System.out.println(baseTen);
    }

  }
}
  • Integer.parseInt can already parse octal values on its own. https://stackoverflow.com/a/11377988/2308683 – OneCricketeer Nov 21 '17 at 05:25
  • Thank you! Do you think those are used enough to warrant keeping them in my memory or not? – Mark Anderson Nov 21 '17 at 05:43
  • Not sure what you mean. Builtin methods are probably better than your own implementation – OneCricketeer Nov 21 '17 at 05:44
  • If you have the octal number `4567`, what values do you need to add together--that is, what does the `7` represent, what does the `6` represent, and so on? What values are you actually adding with `Math.pow(tempDub,power)`? Since your program prints them out, you should be able to see what those values are and why they're wrong. – ajb Nov 21 '17 at 05:57

2 Answers2

0
import java.util.Scanner;

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

    System.out.println("Enter octal number");
    int octal = s.nextInt();

    // you can add some method to check if the input is an octal number here...

    int decNumber = octToDec(octal);
    System.out.println(decNumber);
}

static int octToDec(int n){
    int i = 0;
    int decimal = 0;

    while(n != 0){
        decimal = decimal + (n % 10) * (int) Math.pow(8, i);
            n = n/10;
            i++;
        }

        return decimal;
    }
}
0

You can use this way:

    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);

Or algorithmic way

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;
    }
 }
Son Tieu
  • 517
  • 4
  • 15