You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
992 B

import java.util.*;
import java.io.*;
public class StockPrices
{
public static void main( String[] args )
{
double total = 0.0;
double current;
String line;
File portfolio_txt = new File("portfolio.txt");
Scanner inputFile = null;
// Use a loop in the try block to read each line of the file.
// Lecture Slides 13 (posted on D2L) provide a good example
try
{
inputFile = new Scanner(portfolio_txt);
while (inputFile.hasNextLine()) {
line = inputFile.nextLine();
String[] parts = line.split(" ");
current = Double.parseDouble(parts[1]);
total += current;
}
}
catch(FileNotFoundException fnfe)
{
System.out.println("Can't find file");
// A FileNotFoundException needs to be handled here
}
finally
{
inputFile.close();
}
System.out.println( "The total value is: $" + total );
}
}