Guest User

Untitled

a guest
Dec 12th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. package javameanmedian;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7.  
  8. public class JavaMeanMedian {
  9. static final String FILEPATH = "C:\\Users\\cghue\\Desktop\\"; // path name where file is located
  10.  
  11. public static void main(String[] args) {
  12. // variables to control the disk file
  13. Scanner infile; // input file object
  14. String filename; // name of the file
  15. int recordCount = 0; // number of records (lines) in the file
  16. int recordsToSkip = 0; // used when computing the median
  17. // variables for fields of each record in the file
  18. String lineFromFile; // used when reading a line at a time from file
  19. int acctNo = 0; // account number from a record in the file
  20. String customer = ""; // customer name from a record in the file
  21. double acctBal = 0.0; // account balance from a record in the file
  22. // variable used to determine the mean and median
  23. double total = 0; // used when computing the mean
  24. double mean = 0; // mean (average) of all account balances
  25. double median = 0.0; // median value of account balances
  26.  
  27.  
  28. // create the Scanner objects for the keyboard and disk access
  29. Scanner stdin = new Scanner(System.in);
  30.  
  31. // get just the name of the file from the keyboard
  32. // build the pathname and filename
  33. // identify the folder where the file is located
  34. // The User name is in the environment variable USER on MacOS and Linux
  35. // but it is in the environment variable USERNAME on Windows
  36. System.out.printf("Enter the name of the data file: ");
  37. filename = stdin.nextLine(); // read the filename from standard in
  38. String loginID = System.getenv("USER"); // MacOS or Linux?
  39. if (loginID == null) loginID = System.getenv("USERNAME"); // Windows?
  40. String BalancesFileName = FILEPATH + filename;
  41. double medianBalance = 0.0;
  42. try {
  43. // ---- PART 1, Count the number of records in the file
  44. // Determine the mean when you know the
  45. // record count and the total of all balances
  46. infile = new Scanner(new File(BalancesFileName));
  47.  
  48. while (infile.hasNextInt()) // while there is still data in the file
  49. {
  50. acctNo = infile.nextInt(); // read int for account number
  51. customer = infile.next(); // read string for customer name
  52. acctBal = infile.nextDouble(); // read double for acct balance
  53. total += acctBal;
  54. recordCount++;
  55. }
  56. infile.close();
  57. System.out.printf("%d Records in %s\n", recordCount, filename);
  58. mean = total / recordCount;
  59.  
  60. // ---- PART 2, Determine the number of records to skip
  61. if (recordCount % 2 == 1)
  62. recordsToSkip = recordCount/2; // Odd number of records
  63. else
  64. recordsToSkip = recordCount/2 - 1; // Even number of records
  65.  
  66. // --- PART 3, open the file, skip leading records, determine median
  67. infile = new Scanner(new File(BalancesFileName));
  68.  
  69. // Use an ArrayList to store the account balances
  70. ArrayList<Double> balances = new ArrayList<>();
  71.  
  72. for (int i = 0; i < recordsToSkip; i++) {
  73. acctNo = infile.nextInt();
  74. customer = infile.next();
  75. acctBal = infile.nextDouble();
  76. balances.add(acctBal);
  77. }
  78.  
  79. // Read the remaining balances and add them to the ArrayList
  80. while (infile.hasNextDouble()) {
  81. acctBal = infile.nextDouble();
  82. balances.add(acctBal);
  83. }
  84.  
  85. // Sort the ArrayList
  86. Collections.sort(balances);
  87.  
  88.  
  89. // Calculate the median
  90. int midIndex = (recordCount) / 2;
  91. double midValue;
  92.  
  93. if (recordCount % 2 == 1) {
  94. midValue = balances.get(midIndex);
  95. median = midValue;
  96. }
  97.  
  98. // end of try block
  99. System.out.printf("\nThe mean of %s is %.2f\n\n", filename, mean);
  100. System.out.printf("The median of %s is %.2f\n\n", filename, median);
  101. System.out.println("MidIndex: " + midIndex);
  102. System.out.println("Median Balance: " + medianBalance);
  103.  
  104. } catch (IOException ioe) {
  105. System.out.println("Exception occurred opening or reading " + BalancesFileName);
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment