reading doubles in a text file

I'm having problems get my program to read doubles in a text file and adding them up to be displayed to a user.

Any usggestions?

import javax.swing.*;

import java.io.*;

import java.util.*;

public class Order1

{

public static void main(String[] arg)

throws FileNotFoundException

{

double sum = 0;

double price;

String output;

String str;

Scanner inFile =

new Scanner(new FileReader ("f:\\invoice.dat"));

while(inFile.hasNext())

{

price = inFile.nextDouble();

sum = sum + price;

inFile.nextDouble();

}

output = String.format("The total value of the items in the data file is $ %.2f", sum);

JOptionPane.showMessageDialog(null, output,

"File Totals",

JOptionPane.INFORMATION_MESSAGE);

inFile.close();

System.exit(0);

}

}

[901 byte] By [Raziela] at [2007-10-2 13:42:34]
# 1
What problems, exactly, are you having?
tsitha at 2007-7-13 11:37:16 > top of Java-index,Java Essentials,New To Java...
# 2
He appears to be skipping every other double.
MLRona at 2007-7-13 11:37:16 > top of Java-index,Java Essentials,New To Java...
# 3
the program is to read all the data from a file and total the price of all of the items. It will compile but not display the out put I want. It just display $0.00.
Raziela at 2007-7-13 11:37:16 > top of Java-index,Java Essentials,New To Java...
# 4
I would throw some debug in there (and correct your calling nextDouble twice as Monica pointed out). Maybe display the value that you get for price in your loop to make sure it's what you think it is, then go from there.Good LuckLee
tsitha at 2007-7-13 11:37:16 > top of Java-index,Java Essentials,New To Java...
# 5

I redid the program and it is still not giving me the output I wanted.

import javax.swing.*;

import java.io.*;

import java.util.*;

public class Order1

{

public static void main(String[] arg)

throws FileNotFoundException

{

double sum = 0;

double price = 0;

String output;

String str;

Scanner inFile =

new Scanner(new FileReader ("f:\\invoice.dat"));

while(inFile.hasNextDouble())

{

sum += inFile.nextDouble();

}

output = String.format("The total value of the items in the data file is $ %.2f", sum);

JOptionPane.showMessageDialog(null, output,

"File Totals",

JOptionPane.INFORMATION_MESSAGE);

inFile.close();

System.exit(0);

}

}

Raziela at 2007-7-13 11:37:16 > top of Java-index,Java Essentials,New To Java...
# 6

Seems to be OK when I run it. My file had 1.141<new line>2.254<newline>

and I got a dialog saying "The total value of the items in the data file is

$ 3.40".

Perhaps you should follow tsith's advice: this will show if you are using

data that's not what you were expecting. Like:while(inFile.hasNextDouble())

{

price = inFile.nextDouble();

System.out.println("Added: " + price);

sum += price;

}

pbrockway2a at 2007-7-13 11:37:16 > top of Java-index,Java Essentials,New To Java...
# 7
what output were you looking for? Seems to work.
johnny12a at 2007-7-13 11:37:16 > top of Java-index,Java Essentials,New To Java...