File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Homework help... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Homework help..." Watch "Homework help..." New topic
Author

Homework help...

Trevor Charbonneau
Greenhorn

Joined: Sep 13, 2007
Posts: 3
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?

Thanks so much in advance!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

First of all, welcome to JavaRanch!
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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Trevor Charbonneau
Greenhorn

Joined: Sep 13, 2007
Posts: 3
Yeah, after doing some more searching myself I found that stripTrailingZeros() method.

So I got rid of that part2 part and instead and changed my return value to this:



After I do this, now it says that it can't resolve the "stripTrailingZeros()" method...any ideas?

BTW this in bluej.
[ September 13, 2007: Message edited by: Trevor Charbonneau ]
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
The stripTrailingZeros() method has only been in java since Java 5. Do you know what version of java you are working with, in BlueJ?

- Adam
Trevor Charbonneau
Greenhorn

Joined: Sep 13, 2007
Posts: 3
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 ]
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
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.

- Adam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Homework help...
 
Similar Threads
multiplying large "negative exponents"?
Convert decimal value to binary
Help with nested loop
HOW TO COMMUNICATE JAVA APPLICATION TO SERVLETS
Runtime Error - Need Help