Formatting Double in JOptionPane.showMessageDialog()
807600Oct 11 2007 — edited Oct 11 2007Title says it. I am having issues formatting a double which is being read in from a file. This is being read in.
hammer
9.95
saw
20.15
shovel
35.40
at the moment my code looks like:
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Find1
{
public static void main(String[] args)
throws FileNotFoundException
{
String input1;
double input2;
int a ;
Scanner inFile =
new Scanner(new FileReader("C:\\Documents and Settings\\Corey\\jbproject\\Lab6\\input.txt"));
input1 = JOptionPane.showInputDialog("Please enter the name of item:");
while (inFile.hasNext() || inFile.hasNextDouble())
{
a = input1.compareToIgnoreCase(inFile.next());
if (a == 0)
{
input2 = inFile.nextDouble( );
JOptionPane.showMessageDialog(null,"The " + input1 + " is available for: $" + input2 , "Found", JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
I need input2 to print out in the message box with two decimal places (for shovel 35.40). It works fine for hammer and saw. However, for shovel it doesn't. It drops the zero. Any idea would be appreciated.