The Artima Developer Community
Sponsored Link

Java Answers Forum
'{' expected .. error. Need Help With This Code.

4 replies on 1 page. Most recent reply: Sep 13, 2002 10:58 AM by Brook

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Brook

Posts: 12
Nickname: brook
Registered: Sep, 2002

'{' expected .. error. Need Help With This Code. Posted: Sep 12, 2002 5:16 PM
Reply to this message Reply
Advertisement
I get one error message, at present, and it's '{' expected at line 5. Below is the code. I am simply trying to add the numbers together then print them in currency form. HELP!

import java.math.*;
import java.text.*;
import java.util.*;

public class Add.java {
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(nickel + dimes + quarters);

NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
double money = Total.doubleValue ();
String s = n.format(money);
System.out.println("Formatted: " + S);

}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: '{' expected .. error. Need Help With This Code. Posted: Sep 12, 2002 10:53 PM
Reply to this message Reply
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);
   }
}

Brook

Posts: 12
Nickname: brook
Registered: Sep, 2002

Re: '{' expected .. error. Need Help With This Code. Posted: Sep 13, 2002 9:24 AM
Reply to this message Reply
Thanks for your help as I am NEW to Java. You mention the "file name" should be Add.java . Where does that go in the code and, is anything else included with it? I really don't know.

import java.math.*;
import java.text.*;
import java.util.*;
Add.java //WOULD THIS BE CORRECT?

public class Add
{
public static void main(Sting[] args)
{

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: '{' expected .. error. Need Help With This Code. Posted: Sep 13, 2002 9:52 AM
Reply to this message Reply
Brook,
whatever code Matt has written copy and paste it in notepad and save that file as "Add.java". You don't indicate your file name in your code.
Thanks
Kishori

Brook

Posts: 12
Nickname: brook
Registered: Sep, 2002

Re: '{' expected .. error. Need Help With This Code. Posted: Sep 13, 2002 10:58 AM
Reply to this message Reply
Thanks, I'll give that a try. I was thinking that was where the file name would be, but wanted to make certain.

Flat View: This topic has 4 replies on 1 page
Topic: jsp:forward Problem Previous Topic   Next Topic Topic: JFileChooser

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use