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

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import java.util.*;
  2. import java.io.*;
  3. public class StockPrices
  4. {
  5. public static void main( String[] args )
  6. {
  7. double total = 0.0;
  8. double current;
  9. String line;
  10. File portfolio_txt = new File("portfolio.txt");
  11. Scanner inputFile = null;
  12. // Use a loop in the try block to read each line of the file.
  13. // Lecture Slides 13 (posted on D2L) provide a good example
  14. try
  15. {
  16. inputFile = new Scanner(portfolio_txt);
  17. while (inputFile.hasNextLine()) {
  18. line = inputFile.nextLine();
  19. String[] parts = line.split(" ");
  20. current = Double.parseDouble(parts[1]);
  21. total += current;
  22. }
  23. }
  24. catch(FileNotFoundException fnfe)
  25. {
  26. System.out.println("Can't find file");
  27. // A FileNotFoundException needs to be handled here
  28. }
  29. finally
  30. {
  31. inputFile.close();
  32. }
  33. System.out.println( "The total value is: $" + total );
  34. }
  35. }