1

This is my last lab of this set for while/do-while loops and I've got it all figured out aside from my result printing out in the exact reverse of what it should be.

public class TenToAny
{
private int base10;
private int newBase;

public TenToAny()
{

}

public TenToAny(int ten, int base)
{
   base10 = ten;
   newBase = base;
}

public void setNums(int ten, int base)
{
   base10 = ten;
   newBase = base;
}
public String getNewNum()
{
    String newNum="";
    int orig = base10;

    while(orig > 0)
    {
        newNum = orig%newBase + newNum;
        orig = orig/newBase;
    }
    return newNum;
}

public String toString()
{
    String complete = base10 + " base 10 is " + getNewNum() + " in base " + newBase;

    return complete;
}


}

here's what my results should be:

234 base 10 is 280 in base 9

100 base 10 is 1100100 in base 2

these are the expect results for the first two values (234 in base 9 AND 100 in binary)

Here's what I'm getting:

234 base 10 is 082 in base 9

100 base 10 is 0010011 in base 2

tckmn
  • 57,719
  • 27
  • 114
  • 156
tech_geek23
  • 229
  • 2
  • 10
  • 23

1 Answers1

0

You're simply adding the end of the newNum instead of prepending.

newNum = newNum + orig%newBase;

... should be ...

newNum = orig%newBase + newNum;

For characters...

When orig%newBase > 9, then char(55 + orig%newBase)

var nextValue = orig % newBase;

if (nextValue > 9)
{
    newNum = char(55 + nextValue) + newNum;
}
else
{
    newNum = nextValue + newNum;
}
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • "250 base 10 is FA in base 16" is one of the expected answers.... how would I go about getting the letters for this code? I've never had to deal with letters when doing numeral bases – tech_geek23 Nov 01 '12 at 22:40
  • @tech_geek23: updated the answer. Also, for base 2,8,10,16: [Convert.ToString](http://msdn.microsoft.com/en-us/library/14kwkz77.aspx) – Austin Salonen Nov 01 '12 at 22:55
  • where in my code would this need to be placed? within the while loop? – tech_geek23 Nov 01 '12 at 23:01
  • would you mind editing my original code from this post to show me where it belongs? I don't quite follow what you're saying 100% – tech_geek23 Nov 01 '12 at 23:05
  • now i've got an error saying "Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token "char", delete this token" – tech_geek23 Nov 01 '12 at 23:19