Advertisement
jPV09

BaseballStats.java

Oct 31st, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. public class BaseballStats
  4. {
  5. //-------------------------------------------------
  6. // Reads baseball stats from a file and counts
  7. // total hits, outs, walks, and sacrifice flies
  8. // for each player.
  9. //-------------------------------------------------
  10. public static void main (String[] args) throws IOException
  11. {
  12. Scanner fileScan, lineScan;
  13. String fileName;
  14. int oCount=0, hCount=0, sCount=0, wCount=0;
  15. Scanner scan = new Scanner(System.in);
  16.  
  17. System.out.print ("Enter the name of the input file: ");
  18. fileName = scan.nextLine();
  19. fileScan = new Scanner(new File(fileName));
  20.  
  21. // Read and process each line of the file
  22.  
  23. while (fileScan.hasNext())
  24. {
  25. fileName=fileScan.nextLine();
  26. //System.out.println (" " +fileName);
  27.  
  28. lineScan = new Scanner (fileName);
  29. lineScan.useDelimiter(",");
  30.  
  31.  
  32.  
  33. for (int i=0; i< fileName.length(); i++)
  34. {
  35. if (fileName.charAt(i) == 's')
  36. {
  37. sCount++;
  38. }
  39. else
  40.  
  41. if (fileName.charAt(i) == 'o')
  42. {
  43. oCount++;
  44. }
  45. else
  46.  
  47. if(fileName.charAt(i) == 'h')
  48. {
  49. hCount++;
  50. }
  51. else
  52.  
  53. if (fileName.charAt(i) == 'w')
  54. {
  55. wCount++;
  56. }
  57.  
  58. }
  59.  
  60. System.out.println(lineScan.next()+": Walks: "+wCount+", Hits:"
  61. + " "+hCount+", Sacrifice: "+sCount+", Outs: "+oCount+", "
  62. + "Batting average: "+((double)hCount/(double)(hCount+oCount)));
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement