Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: '{' expected .. error. Need Help With This Code.
|
Posted: Sep 12, 2002 10:53 PM
|
|
You have a whole raft of problems: - You haven't made it clear what your intent is (why is pennies set to ".10", etc.)? - Your placement of curily braces is incorrect, they should be as in the following example. - Your calss should be called Add and the file name should be Add.java (this was the cause of the compiler error you mentioned; javac wanted to see an open curly, not a dot after the class name). - You had typos with variable names (nickels/nickel). - You were using the + operator on objects (what do you think this is, C++?). - You declared a variable with one case and used it with another (what do think this is, Delphi?). Try this:
import java.math.*;
import java.text.*;
import java.util.*;
public class Add
{
public static void main(String[] args)
{
BigDecimal pennies = new BigDecimal(".10");
BigDecimal nickels = new BigDecimal(".20");
BigDecimal dimes = new BigDecimal(".50");
BigDecimal quarters = new BigDecimal("2.00");
BigDecimal Total = pennies.add(nickels.add(dimes.add(quarters)));
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
double money = Total.doubleValue();
String s = n.format(money);
System.out.println("Formatted: " + s);
}
}
|
|