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
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;
}
Use the shift functionality. Rather than using <1000 etc.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
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.
This helper method may be of help:
int strangeDecimalAsBaseN(int number, int base) {
if (base < 2 || base > 10) throw new InvalidArgumentException("Impossible or unsupported base " + base);
if (number < 0) throw new InvalidArgumentException("Negative number (" + number + ") not supported");
int result = 0;
int shift = 1;
while(number > 0) {
int lastResult = result;
result += shift * (number % base);
if (lastResult > result) throw new IllegalStateException("Overflow!");
shift *= 10;
number /= base;
}
return result;
}
Or perhaps this, if you actually wanted it the other way:
int strangeFixBaseN(int funnyNumber, int base) {
if (base < 2 || base > 10) throw new InvalidArgumentException("Impossible base " + base);
if (funnyNumber < 0) throw new InvalidArgumentException("Negative number (" + funnyNumber + ") not supported");
int result = 0;
int shift = 1;
while(funnyNumber > 0) {
int lastResult = result;
result += shift * (funnyNumber % 10);
if (lastResult > result) throw new IllegalStateException("Overflow!");
shift *= base;
funnyNumber /= 10;
}
return result;
}
Beware, untested code :)
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;
}
来源:https://stackoverflow.com/questions/13147109/decimal-to-octal-conversion