3 Replies Latest reply: Nov 9, 2006 6:55 PM by 800351 RSS

    opening and reading a file

    807607
      can someone help??
      import java.util.Scanner;
      import java.io.*;
      
      public class BaseballStats
      {
          //-------------------------------------------------
          //  Reads baseball stats from a file and counts
          //  total hits, outs, walks, and sacrifice flies
          //  for each player.  
          //-------------------------------------------------
          public static void main (String[] args) throws IOException
          {
              Scanner fileScan, lineScan;
              String fileName;
              int countBlank;
              String phrase;    // a string of characters
      
              Scanner scan = new Scanner(System.in);
      
              System.out.print ("Enter the name of the input file: ");
              fileName = scan.nextLine();
              fileScan = new Scanner(new File(fileName));
      
              //look for character h,o,s,and m
              //read and process each line of the file
              while (fileScan.hasNext())
              {
                    fileName=fileScan.nextLine();
                   System.out.println("Baseball Stats " + fileName);
                   
                   fileScan=new Scanner(fileScan);
                   fileScan.useDelimeter("/");
                        
              //print each part
                   while (fileScan.hasNext())
                        System.out.println("     " +fileScan.next());
                             
                             system.out.println();
              }
      }
      
          }
        • 1. Re: opening and reading a file
          800351
          Describe the file/data format of the stats, requirement of your application and the problem now you have encountered.
          • 2. Re: opening and reading a file
            807607
            // ****************************************************************
            //   BaseballStats.java
            //
            //   Reads baseball data in from a comma delimited file. Each line
            //   of the file contains a name followed by a list of symbols
            //   indicating the result of each at bat: h for hit, o for out,
            //   w for walk, s for sacrifice.  Statistics are computed and
            //   printed for each player.
            // ****************************************************************
            
            import java.util.Scanner;
            import java.io.*;
            
            public class BaseballStats
            {
                //-------------------------------------------------
                //  Reads baseball stats from a file and counts
                //  total hits, outs, walks, and sacrifice flies
                //  for each player.  
                //-------------------------------------------------
                public static void main (String[] args) throws IOException
                {   String length;
                     String Scanner;
                    Scanner fileScan;                         //
                    Scanner lineScan;
                    String fileName;
                    int countBlank;
                    int countH=0,countO=0,countS=0,countW=0;
                    String phrase;    // a string of characters
            
                    Scanner scan = new Scanner(System.in);
            
                    System.out.print ("Enter the name of the input file: ");
                    fileName = scan.nextLine();
                    fileScan = new Scanner(new File(fileName));
            
                    //look for character h,o,s,and m
                    //read and process each line of the file
                    while (fileScan.hasNext())
                    {     
                          fileName=fileScan.nextLine();
                         System.out.println("Player Stats: " + fileName);
                         System.out.println();
                    
                         String[] st = fileName.split("h");
                           //countH = fileName.length - 1;
                           //System.out.println (countH);
                         //countH=fileScan.findInLine("h").charAt(0);
                    //     s=Scanner.findInLine("s").charAt(0);
                    //     o=Scanner.findInLine("o").charAt(0);
                    //     w=Scanner.findInLine("w").charAt(0);            
                         
                         lineScan= new Scanner(fileName);
                         lineScan.useDelimiter(","); 
                    //print each part
                    //     while (fileScan.hasNext())
                    //          System.out.println("     " +fileScan.next());
                                   
                    //               System.out.println();
                    }
            }
            
                }
            example of output:
            Enter the name of the input file: stats.dat
            Player Stats: Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h

            Player Stats: Shari Jones,h,o,o,s,s,h,o,o,o,h,o,o,o,o

            I am trying to have it read the number of H, O, S and W's.
            • 3. Re: opening and reading a file
              800351
              Try and study this example code.
              Make an example stats.dat file on your current directory before running this program.
              /* save as AtBats.java */
              /* example stats.dat file **************************************************
              Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h
              Shari Jones,h,o,o,s,s,h,o,o,o,h,o,o,o,o
              Billy Bomb,h,o,h,o,s,o,o,o,h,w,o,o,o,o,s,h,o,h,o,o,h
              Darry Dung,o,o,w,o,o,o,h,w,o,o,o,o,s,h,o,h,o
              Ferris Guilt,o,h,h,o,w,o,o,h,w,o,o,o,o,s,o,h,o,o,o,s
              ****************************************************************************/
              import java.io.*;
              import java.util.*;
              
              public class AtBats{
                ArrayList<Record> records;
                String statsFileName;
              
                public AtBats(String fname){
                  String line;
              
                  statsFileName = fname;
                  records = new ArrayList<Record>();
              
                  try{
                    BufferedReader br = new BufferedReader(new FileReader(statsFileName));
                    while ((line = br.readLine()) != null){
                      records.add(new Record(line));
                    }
                  }
                  catch (Exception e){
                    e.printStackTrace();
                  }
                }
              
                public String toString(){
                  String s = "";
                  String header = String.format("%-20s %10s %10s %10s %10s\n",
                      "name", "outcount", "hitcount", "walkcount", "sacrifices");
                  s += header;
                  for (Record r : records){
                    s += (r.toString() + "\n");
                  }
                  return s;
                }
              
                public static void main(String[] args){
                  String statsfile = "stats.dat";
              
                  if (args.length > 0){
                    statsfile = args[0];
                  }
              
                  AtBats ab = new AtBats(statsfile);
                  System.out.println(ab.toString());
                }
              }
              
              class Record{
                String name;
                int outNum;
                int hitNum;
                int walkNum;
                int sacrNum;
              
                public Record(String statsString){
                  name = getName(statsString);
                  outNum = getOutCounts(statsString);
                  hitNum = getHitCounts(statsString);
                  walkNum = getWalkCounts(statsString);
                  sacrNum = getSacrCounts(statsString);
                }
              
                String getName(String s){
                  return s.substring(0, s.indexOf(',')); // just before first ','
                }
              
                int getOutCounts(String s){
                  return charCount(s, 'o');
                }
              
                int getHitCounts(String s){
                  return charCount(s, 'h');
                }
              
                int getWalkCounts(String s){
                  return charCount(s, 'w');
                }
              
                int getSacrCounts(String s){
                  return charCount(s, 's');
                }
              
                int charCount(String s, char ch){
                  int c = 0;
                  s = s.substring(s.indexOf(',')); // chop off name
                  for (int i = 0; i < s.length(); ++i){
                    if (s.charAt(i) == ch){
                      ++c;
                    }
                  }
                  return c;
                }
              
                public String toString(){
                  return String.format
                    ("%-20s %10d %10d %10d %10d", name, outNum, hitNum, walkNum, sacrNum);
                }
              }