PDA

View Full Version : Java DecimalFormat Question



s3a
October 17th, 2009, 10:33 PM
Here is my code:


//import javax.swing.JOptionPane;
import java.text.DecimalFormat;

import java.util.Scanner;

public class decform_checker
{
public static void main (String [] args)
{
Scanner kb = new Scanner(System.in);

double number = kb.nextDouble;
//double number = 268.324825;

DecimalFormat formatter = new DecimalFormat("#0000.000");
System.out.println(formatter.format(number));
}
}

If I were to uncomment that line that is bold, and comment the other line that is bold, my program would work but at the moment it does not! Could someone please tell me what I am doing wrong? (the compilation error says that it doesn't see a symbol on the line I have bold that is not commented)

Thanks in advance!

Reiger
October 17th, 2009, 11:24 PM
'tis a simple matter of spelling. Or reading what you type, really.

It may be myself who has a bit too much experience as an idiot pounding a problem for hours before realizing its a genuine typo that does it and a simple fix takes a second -- but I would really have expected you to take some time and notice the striking absence of the parentheses within roughly 5 seconds of evaluation.

EDIT: Helpful note: if a compiler -or indeed, any kind of tool related to programming- complains about missing symbols it means you made a typo or forgot to import those symbols (typically pieces of library code). A compiler basically treats any program as a sequence of ‘things’ that it calls ‘symbols’ but most humans would probably call ‘names’ because 95% of the time that is what they are. However, symbols extend beyond names -- they include any piece of syntax (such as ‘keywords’ and ‘operators’ -- even including parentheses).

Similarly ‘invalid’ or ‘symbol not allowed here’ or ‘syntax errors’ problems mean that you are sure typing something but it is not this formal programming language as known by the compiler/tool.

pbrockway2
October 18th, 2009, 12:16 AM
So look at the symbol that the compiler is telling you about.

Look it up in the API documentation. Have you spelt it correctly? Is the cApItAlIsAtIoN correct? If it is a method, does it have the right number of arguments (of the right sort and in the right order)?

s3a
October 18th, 2009, 02:29 AM
Which parentheses? I still can't see my mistake. I did not know that when the compiler complains about symbols that the problem is usually a typo...that is useful advice so thanks for that but I'm still unable to spot my mistake.

pbrockway2
October 18th, 2009, 02:34 AM
Which parentheses?

The parentheses that aren't there! ;)

nextDouble is a method, right? So...

s3a
October 18th, 2009, 03:49 AM
Oh, I get it now! I can't believe I didn't see that! Thanks to all of you!