Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Exercise 86

Hi, I'm having a little trouble with exercise 86. I could totally use some help. Anyone think I'm off to a good start? What do I need to accomplish? I think it has to do with rounding...

Here are the instructions:

```
Complete the following definitions for the overloaded one-argument static method money that returns a String representing its argument as an amount of money. For example,

if the input is the double 2.5, then it returns the String "\$2.50";

if the input is the int 6, then it returns the String "\$6.00"; and

if the input is the String "6.125" then it returns the String "\$6.13".

Hint: Define the money( double d ) version first. Then define the other versions by changing their argument into a double and returning the result of applying the double version to the transformed argument.

Note: When the input is a String you may need to use Double.parseDouble.```

Here is my code:

```
public static String money( int n )

{

// ...

}

public static String money( double d )

{

money *= 100;

money += 5;

money /= 100;

// ...

}

public static String money( String s )

{

// ...

}

public static void main( String[] args )

{

System.out.println( money( 2.538 ) );

}```

Thanks for the help! I really appreciate it!

2. ## Re: Exercise 86

Can you explain what the problems are?

3. ## Re: Exercise 86

try to parse your input to double (Double.parseDouble(input)) if input is string,, cast it to double if input is int..
then after that,, use DecimalFormat class,, it can format your double value the way you want,, in your case
you want to format your double variable into 2 decimal only,, I hope it helps you

--- Update ---

here is a sample code,, it will satisfy your conditions, i believe..
i just made it static,, but please avoit using static if its not necessa.. tnx

```private static String money(String input) {
DecimalFormat df = new DecimalFormat("\$0.00");
return df.format(Double.parseDouble(input));
}

private static String money(int input) {
DecimalFormat df = new DecimalFormat("\$0.00");
return df.format((double) input);
}

private static String money(double input) {
DecimalFormat df = new DecimalFormat("\$0.00");
return df.format(input);
}```

4. ## Re: Exercise 86

@dicdic Please do NOT do the OPs homework.

5. ## Re: Exercise 86

@dicdic

I tried it your way and got this error:
--------------------------------------------------------

"cannot find symbol"

--------------------------------------------------------

But that doesn't mean that your code won't work. I think it might work fine, but I didn't really want someone to GIVE me the answer. Now that I already have the code though, I think it would make sense to modify it a little further. I really wanted clues and hints as opposed to a definite answer since this is for school.

Thanks anyways though. Your code doesn't completely work. But thanks.

--- Update ---

@Norm:

It's giving me errors that say "cannot find symbol. I also think there are compilation issues.

6. ## Re: Exercise 86

Please copy the full text of the error message and paste it here. It has important info about the error.

7. ## Re: Exercise 86

Alright, I lost my old code, which I should have emailed to myself. But when I put it back together, here is the error message. This is pretty much dicdic's code, but I'm going to modify it again anyways, so whatever.

Here is the error message:

```
TC1.java:7: error: cannot find symbol

DecimalFormat df = new DecimalFormat("0.00");```

--- Update ---

I will. Thanks.

--- Update ---

I changed the first part of the code a little bit. Tell me if this is on the right track:

```
public static String money(String money, double dollars)

{

double money = Math.round(dollars * 100) / 100;

}

public static String money(int input)

{

DecimalFormat df = new DecimalFormat("0.00");

return ("\$" + df.format((double) input));

}

public static String money(double input)

{

DecimalFormat df = new DecimalFormat("0.00");

return df.format(input);

}

public static void main( String[] args )

{

System.out.println( money( 2.538 ) );

}```

I think I've been editing it to look like this part:

```
public static String money(String money, double dollars)

{

double money = Math.round(dollars * 100) / 100;

}```

Is there a consistent way of using that method throughout the whole code?

As is I get this, but I THINK (I'm not sure) that means that I'm going to have to come up with new arguments:

```
TC1.java:7: error: variable money is already defined in method money(String,double)

double money = Math.round(dollars * 100) \ 100;```

8. ## Re: Exercise 86

Alright, I lost my old code . . .
Dog ate it?
Is there a consistent way of using that method throughout the whole code?
What does that mean? But I think the answer's "Yes."
I THINK (I'm not sure) that means that I'm going to have to come up with new arguments
No, it means you should remove the 'double' declaration from the line in the body of the method. That's also a method defined to return a String object with no return, so that'll be the next problem with that method. Maybe your practice of posting methods with huge gaps of blank lines indicates there's more code there, but I'm not sure.

9. ## Re: Exercise 86

Originally Posted by GregBrannon
Dog ate it?
No, I forgot to email it to myself. This AP online course won't let me save my code usually.

Originally Posted by GregBrannon
What does that mean? But I think the answer's "Yes."
I mean, can I use just the math.round method to do this throughout the entire code?

Originally Posted by GregBrannon
No, it means you should remove the 'double' declaration from the line in the body of the method. That's also a method defined to return a String object with no return, so that'll be the next problem with that method. Maybe your practice of posting methods with huge gaps of blank lines indicates there's more code there, but I'm not sure.
I think I fixed that. Here's my new code so far. I don't know what to put in the middle:

```
public static String money(String money, double dollars){

public static String money(int dollars){

money = Math.round(dollars * 100) / 100.0;
}
public static String money(input){

}
public static void main( String[] args )
{
System.out.println( money( 2.538 ) );
}
}```

10. ## Re: Exercise 86

"cannot find symbol"
did you import DecimalFormat?? if not,, it will obviously not known when you compile it..
you need to import it first from import java.text.DecimalFormat..
the "Math" class youve been using came from the core package, that is why you
dont need to import it..
import java.text.DecimalFormat

11. ## Re: Exercise 86

Why do you have a method inside another method?
Why do you have a parameter without a type?
Why do you have a method with 2 parameters (String, double)?
Instructions state there should only be two methods: one takes a double and the other takes an int (which calls the double method).

12. ## Re: Exercise 86

Okay that makes sense. Thanks. I'll submit some more code pretty soon.