For my programming class, we needed to create a project which does exponents. The only thing was that we would get some big exponents and we had to get around the fact the double can only hold values up to a certain length.
We were then given a set of tests in which our program had to pass.
This is my program as it stands now:
This is my code as it stands now. With the out the part two part I would pass all but 6 tests. After debugging we found it was because our program kept the trailing zeros. So I added that part2 part.
I am pretty sure that would work, but when I compile it says that: "cannot resolve symbol - method length()"
Is there any way I can get around that? Is there an easier way to trim off the trailing zeros?
Originally posted by Trevor Charbonneau: I am pretty sure that would work, but when I compile it says that: "cannot resolve symbol - method length()"
In your "PART TWO" you are treating your BigDecimal as if it is a String - you're trying to call methods like length() and charAt() on it, but that won't work, because it's not a String, and class BigDecimal doesn't have methods like length() and charAt().
What is it exactly that you're trying to do? Note that BigDecimal has a method stripTrailingZeros() (since Java 5), maybe that's what you're looking for?
The stripTrailingZeros() method has only been in java since Java 5. Do you know what version of java you are working with, in BlueJ?
Joined: Sep 13, 2007
Crap it is only 1.4...crap.
I can't install eclipse on this comp.
If I were to load it onto eclipse would it work? [ September 13, 2007: Message edited by: Trevor Charbonneau ]
Joined: Jul 17, 2006
Not necessarily. You still need to make sure its using Java 1.5.
The more relevent question is this: Will your teacher accept a program written in 1.5. If the computers at your school are only equipped with version 1.4, then your teacher may require you to use version 1.4.
However, there are still intelligent ways to strip out the zeros using 1.4.
The code you had before, although not terribly efficient (in fact, terribly inefficient), would have worked. BUT, you need to convert the number to a string FIRST, then check if the character at each position of the string is equal to '0'.
However, notice that the substring method is SLOW. What you might want to consider doing instead is to use an integer variable to store the last location that is not a zero (same if condition as you're using, just instead of calling substring, assign the value of g to another variable).
Then, when you've finished the loop, you make a single substring call using the value stored in the variable. This will be much more efficient.