Results 1 to 18 of 18
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Norm

Thread: problem to open .txt with different types of token in each line

  1. #1
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default problem to open .txt with different types of token in each line

    hi my problem is that i have 4 file classes. their names are Player, BaseballPlayer, Pitcher, SoccerPlayer and Goalie... so what i have to do is use extends so Player is the Parent class... now my 5 class is the main where i need to open a txt file that has this format:

    Players.txt is formatted as follows:

    Soccer Players:
    sport team position firstName lastName minutes goalsScored

    Goalies:
    Sport team position firstName lastName minutes goalsScored goalsAllowed

    Baseball Players:
    sport team position firstName lastName atBats hits

    Pitchers
    sport team position firstName lastName atBats hits innings earnedRuns

    Example:
    Soccer Chicago Midfield Justin Mapp 1866 2
    Soccer Chicago Goalie Zach Thornton 2160 0 30
    Baseball Mets Right Xavier Nady 265 70
    Baseball Mets Pitcher Tom Glavine 53 9 198 84

    so my problem is that my main class need to have 2 methods one for open the files and one for reading the files but i do not know how to read the files because there is one line that can have more tokens than the other i was thinking about use an if statement looking for when it is goalie, when it is just a soccer player, when it is a baseballPlayer or when it is a pitcher using inheritance but i do not know how to do it... so far this is my main class:

    Java Code:
    package HomeWork7;
    
    import java.io.*;
    import java.util.*;
    
    /**
     *
     * @author Elvis De Abreu
     */
    public class PlayersMain 
    {
        private ArrayList<Player> theplayer = new ArrayList<Player>();
        Scanner in;
        PrintWriter out;
        
        
        public void openFiles()
        {
        try
            {
                File input = new File("Players.txt");
                in = new Scanner(input);
          
                File output = new File("output.txt");
                out = new PrintWriter(output);      
            }
        catch (FileNotFoundException ex) 
            {
            System.err.println("File not found!");
            ex.printStackTrace();
            }
        }
        
        public void readData()
        {
        	String sport, team, pos, fn, ln;
        	int a;
            double b;
            int c;
            Player p;
            
            while(in.hasNext())
            {
              sport = in.next();
              team = in.next();
              pos = in.next();
              fn = in.next();
              ln = in.next();
              a = in.nextInt();
              b = in.nextDouble();
              c = in.nextInt();
            }
            
            closeFiles();
        }
     
      /**
       * close the files that are been used in the program
       * the output file and the input file
       */
      public void closeFiles()
      {
        in.close();
        out.close();
      }
       
      public BaseballPlayer getHighBattingAverage()
      {
    	return null;
          
      }
      
      public Goalie getLowestAvgGoalsAllowed()
      {
    	return null;
          
      }
      
      public void displayPlayers(ArrayList<Player> list)
      {
    	  openFiles();
    	    
    	    for(Player e: list)
    	    {
    	      out.println(e.getPosition() + ": " + e.getName());
    	    }
    	    
    	    closeFiles();
      }
        public static void main(String[] args) {
            PlayersMain pm = new PlayersMain();
            pm.openFiles();      //opens the input and output files
            pm.readData();       //reads the data file and populates the arraylist
            BaseballPlayer b = pm.getHighBattingAverage();
            System.out.println(b);
            Goalie g = pm.getLowestAvgGoalsAllowed();
            System.out.println(g);
            pm.displayPlayers(); //output all players in the arraylist to the file
            pm.closeFiles();     //close input and output files
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: problem to open .txt with different types of token in each line

    how to read the files because there is one line that can have more tokens than the other
    Is there a token on the line that will tell you how many more tokens there are on that line?
    Use an if statement to detect that token and then change the logic for the number of remaining tokens on that line.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,746
    Rep Power
    19

    Default Re: problem to open .txt with different types of token in each line

    Quote Originally Posted by elvis0288 View Post
    so my problem is that my main class need to have 2 methods one for open the files and one for reading the files but i do not know how to read the files because there is one line that can have more tokens than the other i was thinking about use an if statement looking for when it is goalie, when it is just a soccer player, when it is a baseballPlayer or when it is a pitcher using inheritance but i do not know how to do it...
    This is the sort of thing that could do with a factory method(s).
    Read a line in, and pass that String to the method.
    The method returns a Player.

    Inside the method, write the logic that determines what sort of Player to create.
    You should be able to determine this with the first and third entries in the line.

    Note, I really wouldn't use Scanner for this.
    Use a BufferedReader to get the lines.
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default Re: problem to open .txt with different types of token in each line

    Note, I really wouldn't use Scanner for this.
    Use a BufferedReader to get the lines.
    why should i use bufferedReader instead of scanner??

    ok i think i know what are you trying to say so i fix it and i did this

    Java Code:
    public void readData()
        {
        	String sport, team, pos, fn, ln;
        	int goalsScored;
            int goalsAllowed;
            int minutes;
            int atBats;
            int hits;
            double innings;
            int earnedRuns;
            Player p;
          
            while(in.hasNext())
            {
              sport = in.next();
              team = in.next();
              pos = in.next();
              fn = in.next();
              ln = in.next();
              
              if (sport.equalsIgnoreCase("soccer"))
              {
              	minutes = in.nextInt();
              	goalsScored = in.nextInt();
              	if (pos.equalsIgnoreCase("goalie"))
              	{
              		goalsAllowed = in.nextInt();
                            //should i put some code here like g = new goalie(); to iniates the goalie class?
                           //and then do theplayer.add(g)???
              	}
              	
              }
               
            }
            
            closeFiles();
        }
    so now i do not know if that how i did with player p; i have to do the same with goalie g; and pitcher pi; and things like that and inside the if statement initialize each one and then put it in the same array list...

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: problem to open .txt with different types of token in each line

    The readData method should return Player.
    The code inside the if statements should create the class that fits the data for the line that is being scanned.
    You should pass the line to be scanned to the method.
    The Scanner class has a constructor that takes a String.
    Tolls likes this.

  6. #6
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default Re: problem to open .txt with different types of token in each line

    ok i was thinking about what you said and instead of returning I start adding to my array list in this way:
    Java Code:
    public void readData()
        {
        	String sport, team, pos, fn, ln;
        	int goalsScored;
            int goalsAllowed;
            int minutes;
            int atBats;
            int hits;
            double innings;
            int earnedRuns;
          
            while(in.hasNext())
            {
              sport = in.next();
              team = in.next();
              pos = in.next();
              fn = in.next();
              ln = in.next();
              
              if (sport.equalsIgnoreCase("soccer"))
              {
              	minutes = in.nextInt();
              	goalsScored = in.nextInt();
              	if (pos.equalsIgnoreCase("goalie"))
              	{
              		goalsAllowed = in.nextInt();
              		Goalie g = new Goalie(sport,team,pos,fn,ln,minutes,goalsScored,goalsAllowed);
              		theplayer.add(g);
              	}
              	SoccerPlayer socc = new SoccerPlayer(sport,team,pos,fn,ln,minutes,goalsScored);
              	theplayer.add(socc);
              }
              
              if (sport.equalsIgnoreCase("Baseball"))
              {
            	  atBats = in.nextInt();
            	  hits = in.nextInt();
            	  if(pos.equalsIgnoreCase("Pitcher"))
            	  {
            		  innings = in.nextDouble();
            		  earnedRuns = in.nextInt();
            		  Pitcher pit = new Pitcher(sport,team,pos,fn,ln,atBats,hits,innings,earnedRuns);
            		  theplayer.add(pit);
            	  }
            	  BaseballPlayer base = new BaseballPlayer(sport,team,pos,fn,ln,atBats,hits);
            	  theplayer.add(base);
            	  	
              }
               
            }
            
            closeFiles();
        }
    that should work right?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: problem to open .txt with different types of token in each line

    that should work right?
    Have you tried it? Ask the computer.

    The method should take a line as an argument and return a Player object. Your code uses too many global variables.

    There is a slight logic flaw. You have if followed by another if. I'm assuming that When the first is true, the second one should not be true.
    Use if/else if

  8. #8
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default Re: problem to open .txt with different types of token in each line

    thank you man it is working now i have one last problem i am trying to think how to make this i know i have to use instance of or something similar to that but i do not know how to do i need to get the baseball player with higher batting average so i do not know how to make that this is my code:
    i do not know if i have to use like that or how to declare even the method
    Java Code:
    public BaseballPlayer getHighBattingAverage()
      {
    	return null;
          
      }
      
      public Goalie getLowestAvgGoalsAllowed()
      {
    	return null;
          
      }
      
      public void displayPlayers(ArrayList<Player> list)
      {
    	  openFiles();
    	    
    	    for(Player e: list)
    	    {
    	      out.println(e.getPosition() + ": " + e.getName());
    	    }
    	    
    	    closeFiles();
      }
        public static void main(String[] args) {
            PlayersMain pm = new PlayersMain();
            pm.openFiles();      //opens the input and output files
            pm.readData();       //reads the data file and populates the arraylist
            BaseballPlayer b = pm.getHighBattingAverage();
            System.out.println(b);
            Goalie g = pm.getLowestAvgGoalsAllowed();
            System.out.println(g);
            pm.displayPlayers(); //output all players in the arraylist to the file
            pm.closeFiles();     //close input and output files
        }
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: problem to open .txt with different types of token in each line

    get the baseball player with higher batting average
    Use a loop to look at each player's score and save the one with the highest.

  10. #10
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default Re: problem to open .txt with different types of token in each line

    my problem is how i make the program know which one are baseball player and which one are not if all of then are in the same array list should i use if
    Java Code:
    if (p instanceof BaseballPlayer){
        BaseballPlayer s = (Student)p;
        System.out.println();

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,251
    Rep Power
    25

    Default Re: problem to open .txt with different types of token in each line

    That would work.

    What happened when you tried it?

  12. #12
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default Re: problem to open .txt with different types of token in each line

    no it does not work, i am still stuck in how to initialize the arraylist to them make a instance of the class for example this is my baseballPlayer class
    Java Code:
    package HomeWork7;
    
    public class BaseballPlayer extends Player{
        
        private int atBat, hits;
        
        public BaseballPlayer(String sp, String t, String pos,
                String f_name, String l_name, int ab, int h) {
            super(sp, t, pos, f_name, l_name);
            atBat = ab;
            hits = h;
        }
        
        public double battingAverage() {
            return ((double)hits/atBat);
        }
        
        public String toString() {
            return super.toString() + " Position: " + super.getPos() 
            + " Batting Avg: " + String.format("%3.3f", battingAverage()); 
        }
    }
    and this is my main class so far
    Java Code:
    package HomeWork7;
    
    import java.io.*;
    import java.util.*;
    
    /**
     *
     * @author Elvis De Abreu
     */
    public class PlayersMain 
    {
        private ArrayList<Player> theplayer = new ArrayList<Player>();
        Scanner in;
        PrintWriter out;
        
        
        public void openFiles()
        {
        try
            {
                File input = new File("Players.txt");
                in = new Scanner(input);
          
                File output = new File("output.txt");
                out = new PrintWriter(output);      
            }
        catch (FileNotFoundException ex) 
            {
            System.err.println("File not found!");
            ex.printStackTrace();
            }
        }
        
        public void readData()
        {
        	String sport, team, pos, fn, ln;
        	int goalsScored;
            int goalsAllowed;
            int minutes;
            int atBats;
            int hits;
            double innings;
            int earnedRuns;
          
            while(in.hasNext())
            {
              sport = in.next();
              team = in.next();
              pos = in.next();
              fn = in.next();
              ln = in.next();
              
              if (sport.equalsIgnoreCase("soccer"))
              {
              	minutes = in.nextInt();
              	goalsScored = in.nextInt();
              	
              	if (pos.equalsIgnoreCase("goalie"))
              	{
              		goalsAllowed = in.nextInt();
              		Goalie g = new Goalie(sport,team,pos,fn,ln,minutes,goalsScored,goalsAllowed);
              		theplayer.add(g);
              	}
              	SoccerPlayer socc = new SoccerPlayer(sport,team,pos,fn,ln,minutes,goalsScored);
              	theplayer.add(socc);
              }
              
              else if (sport.equalsIgnoreCase("Baseball"))
              {
            	  atBats = in.nextInt();
            	  hits = in.nextInt();
            	  
            	  if(pos.equalsIgnoreCase("Pitcher"))
            	  {
            		  innings = in.nextDouble();
            		  earnedRuns = in.nextInt();
            		  Pitcher pit = new Pitcher(sport,team,pos,fn,ln,atBats,hits,innings,earnedRuns);
            		  theplayer.add(pit);
            	  }
            	  BaseballPlayer base = new BaseballPlayer(sport,team,pos,fn,ln,atBats,hits);
            	  theplayer.add(base);
            	  	
              }
            }
        }
     
      /**
       * close the files that are been used in the program
       * the output file and the input file
       */
      public void closeFiles()
      {
        in.close();
        out.close();
      }
       
      public BaseballPlayer getHighBattingAverage()
      {
    
    	if(something instanceof BaseballPlayer)
    	{
    		//i do not know how to do this how to initialize this of something
    //and then make it work please i need help the array list already have the information
    //how i make it check from each one what is baseball player and what is not i know i need a for loop but i am stuck please help
    	}
          
      }
      
      public Goalie getLowestAvgGoalsAllowed()
      {
    	return null;
          
      }
      
      public void displayPlayers(ArrayList<Player> list)
      {
    	  openFiles();
    	    
    	    for(Player e: list)
    	    {
    	      out.println(e.getPosition() + ": " + e.getName());
    	    }
    	    
    	    closeFiles();
      }
        public static void main(String[] args) {
            PlayersMain pm = new PlayersMain();
            pm.openFiles();      //opens the input and output files
            pm.readData();       //reads the data file and populates the arraylist
            BaseballPlayer b = pm.getHighBattingAverage();
            System.out.println(b);
            Goalie g = pm.getLowestAvgGoalsAllowed();
            System.out.println(g);
            pm.displayPlayers(); //output all players in the arraylist to the file
            pm.closeFiles();     //close input and output files
        }
    }

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,746
    Rep Power
    19

    Default Re: problem to open .txt with different types of token in each line

    Quote Originally Posted by elvis0288 View Post
    why should i use bufferedReader instead of scanner??
    Because each line represents a 'thing' in your system, so you want to read each line.
    BufferedReader provides just that functionality.
    Scanner provides far more than you need, and often results in people trying to do everything using the one reader/scanner.
    So, using BufferedReader you read a line and pass that to your method for creating a Player.
    Otherwise you end up with a mass of code in a method that does far too much, and is far more pronbe to bugs.

    Quote Originally Posted by elvis0288 View Post
    ok i was thinking about what you said and instead of returning I start adding to my array list in this way:
    Java Code:
    public void readData()
        {
        	String sport, team, pos, fn, ln;
        	int goalsScored;
            int goalsAllowed;
            int minutes;
            int atBats;
            int hits;
            double innings;
            int earnedRuns;
          
            while(in.hasNext())
            {
              sport = in.next();
              team = in.next();
              pos = in.next();
              fn = in.next();
              ln = in.next();
              
              if (sport.equalsIgnoreCase("soccer"))
              {
              	minutes = in.nextInt();
              	goalsScored = in.nextInt();
              	if (pos.equalsIgnoreCase("goalie"))
              	{
              		goalsAllowed = in.nextInt();
              		Goalie g = new Goalie(sport,team,pos,fn,ln,minutes,goalsScored,goalsAllowed);
              		theplayer.add(g);
              	}
              	SoccerPlayer socc = new SoccerPlayer(sport,team,pos,fn,ln,minutes,goalsScored);
              	theplayer.add(socc);
              }
              
              if (sport.equalsIgnoreCase("Baseball"))
              {
            	  atBats = in.nextInt();
            	  hits = in.nextInt();
            	  if(pos.equalsIgnoreCase("Pitcher"))
            	  {
            		  innings = in.nextDouble();
            		  earnedRuns = in.nextInt();
            		  Pitcher pit = new Pitcher(sport,team,pos,fn,ln,atBats,hits,innings,earnedRuns);
            		  theplayer.add(pit);
            	  }
            	  BaseballPlayer base = new BaseballPlayer(sport,team,pos,fn,ln,atBats,hits);
            	  theplayer.add(base);
            	  	
              }
               
            }
            
            closeFiles();
        }
    that should work right?
    Oh look.
    See?
    That should be several methods, each with a specific task.
    createPlayer(String).
    createBasketballPlayer(String[]). which will create a pitcher or other.
    createFootballPlayer(String[]), which will create a goalie or other.

    Trying to stick to all into one method is the most common way of complicating code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default Re: problem to open .txt with different types of token in each line

    yeah i know what you mean but the professor want us to open everything in readData method but i will try to do that to see what happen so far i finished and it is working perfect but i will try to do that so it will looks cleaner thank you very much to everybody.. this is how the code look working
    Java Code:
    package HomeWork7;
    
    import java.io.*;
    import java.util.*;
    
    /**
     *
     * @author Elvis De Abreu
     */
    public class PlayersMain 
    {
        private ArrayList<Player> theplayer = new ArrayList<Player>();
        Scanner in;
        PrintWriter out;
        
        
        public void openFiles()
        {
        try
            {
                File input = new File("Players.txt");
                in = new Scanner(input);
          
                File output = new File("output.txt");
                out = new PrintWriter(output);      
            }
        catch (FileNotFoundException ex) 
            {
            System.err.println("File not found!");
            ex.printStackTrace();
            }
        }
        
        public void readData()
        {
        	String sport, team, pos, fn, ln;
        	int goalsScored;
            int goalsAllowed;
            int minutes;
            int atBats;
            int hits;
            double innings;
            int earnedRuns;
          
            while(in.hasNext())
            {
              sport = in.next();
              team = in.next();
              pos = in.next();
              fn = in.next();
              ln = in.next();
              
              if (sport.equalsIgnoreCase("soccer") && pos.equalsIgnoreCase("goalie"))
              {
              	minutes = in.nextInt();
              	goalsScored = in.nextInt();
              	goalsAllowed = in.nextInt();
          		Goalie g = new Goalie(sport,team,pos,fn,ln,minutes,goalsScored,goalsAllowed);
          		theplayer.add(g);
          		
              }
          		else if (sport.equalsIgnoreCase("soccer") && !pos.equalsIgnoreCase("goalie"))
          		{
          			minutes = in.nextInt();
              		goalsScored = in.nextInt();
          			SoccerPlayer socc = new SoccerPlayer(sport,team,pos,fn,ln,minutes,goalsScored);
              		theplayer.add(socc);
          		}
              
              if (sport.equalsIgnoreCase("Baseball") && pos.equalsIgnoreCase("Pitcher"))
              {
            	  atBats = in.nextInt();
            	  hits = in.nextInt();
            	  innings = in.nextDouble();
            	  earnedRuns = in.nextInt();
            	  Pitcher pit = new Pitcher(sport,team,pos,fn,ln,atBats,hits,innings,earnedRuns);
            	  theplayer.add(pit);
              }
              else if (sport.equalsIgnoreCase("Baseball") && !pos.equalsIgnoreCase("Pitcher"))
              {
            	  atBats = in.nextInt();
        	  	  hits = in.nextInt();
            	  BaseballPlayer base = new BaseballPlayer(sport,team,pos,fn,ln,atBats,hits);
            	  theplayer.add(base);
            	  	
              }
            }
        }
     
     
      /**
       * close the files that are been used in the program
       * the output file and the input file
       */
      public void closeFiles()
      {
        in.close();
        out.close();
      }
       
      public BaseballPlayer getHighBattingAverage()
      {
    	  double max = 0;
    	  BaseballPlayer base = new BaseballPlayer();
    	  
    	  for(Player p: theplayer)
    		  if(p instanceof BaseballPlayer)
    		  {
    			  if(((BaseballPlayer) p).battingAverage() > max)
    			  {
    				  max = ((BaseballPlayer) p).battingAverage();
    				  base = (BaseballPlayer) p;
    			  }
    		  }
    	  return base;
      }
      
      public Goalie getLowestAvgGoalsAllowed()
      {
    	  Goalie goal = new Goalie();
    	  double max = 0;
    	  
    	  for(Player e: theplayer)
    	  {
    		  if(e instanceof Goalie)
    		  {
    			  max = ((Goalie) e).averageGoalsAllowed();
    			  break;
    		  }
    	  }
    	  
    	  for(Player p: theplayer)
    	  {
    		  
    		  if(p instanceof Goalie)
    		  {
    			  if(((Goalie) p).averageGoalsAllowed() < max)
    			  {
    				  max = ((Goalie) p).averageGoalsAllowed();
    				  goal = (Goalie) p;
    			  }
    		  }
    	  }
    	  return goal;
          
      }
      
      public void displayPlayers()
      {
    	  	for(Player e: theplayer)
    	    {
    	    	if(e instanceof BaseballPlayer)
    	    	{
    	    		out.println((BaseballPlayer)e);
    	    	}
    	    	if (e instanceof SoccerPlayer)
    	    	{
    	    		out.println((SoccerPlayer)e);
    	    	}
    	    	
    	    }
      }
      
        public static void main(String[] args) {
            PlayersMain pm = new PlayersMain();
            pm.openFiles();      //opens the input and output files
            pm.readData();       //reads the data file and populates the arraylist
            BaseballPlayer b = pm.getHighBattingAverage();
            System.out.println(b);
            Goalie g = pm.getLowestAvgGoalsAllowed();
            System.out.println(g);
            pm.displayPlayers(); //output all players in the arraylist to the file
            pm.closeFiles();     //close input and output files
        }
    }
    last question i need to generate a javadoc i can generated it and open it if i go to the file but is there anyway i can auto open the file from eclipse directly like netbeans? because in netbeans i can just press generate javadoc and it will open the web browser with the javadoc but in eclipse i have to go to the folder to be able to see the javadoc document... thank you

  15. #15
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default Re: problem to open .txt with different types of token in each line

    i found at already thank you for everything do i need to close this post or something?

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,746
    Rep Power
    19

    Default Re: problem to open .txt with different types of token in each line

    There's no reason (in my mind) that readData() couldn't call other methods to do its job.
    They'd be a rather poor professor if they marked you down for breaking down the problem into manageable bits.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    elvis0288 is offline Member
    Join Date
    Jan 2012
    Posts
    21
    Rep Power
    0

    Default Re: problem to open .txt with different types of token in each line

    There's no reason (in my mind) that readData() couldn't call other methods to do its job.
    They'd be a rather poor professor if they marked you down for breaking down the problem into manageable bits.
    you are right so i did what you are talking about this is how it looks... thank you

    Java Code:
    /**
     * PlayersMain is the class where the main is located to run the classes
     * in this class we open the file input and output read the data
     * and use this data to find the player with better batting avg and
     * the player with lowest avg goals allowed
     */
    package HomeWork7;
    
    import java.io.*;
    import java.util.*;
    
    /**
     *
     * @author Elvis De Abreu
     * 
     */
    public class PlayersMain 
    {
    	//variable declaration/initialization
    	//array list of players
        private ArrayList<Player> theplayer = new ArrayList<Player>();
        Scanner in;		//scanner to read from files
        PrintWriter out;	//printwriter to output in a file
        
        /**
         * open the files of the Players.txt and create and open a output file
         * named output.txt
         */
        public void openFiles()
        {
        try
            {
                File input = new File("Players.txt");
                in = new Scanner(input);
          
                File output = new File("output.txt");
                out = new PrintWriter(output);      
            }
        catch (FileNotFoundException ex) 
            {
            System.err.println("File not found!");
            ex.printStackTrace();
            }
        }
        
        /**
         * this methods is used to stored an object when it is a soccer player
         * @param sport is the sport of the player in this case Baseball
         * @param team is the team of the player 
         * @param pos is the position of the player
         * @param fn is the first name of the player
         * @param ln is the second name of the player
         */
        public void BaseballPlayer(String sport, String team, String pos,
        		String fn, String ln)
        {
        	int atBats;				//times at bat
            int hits;				//hits made
            double innings;			//innings played
            int earnedRuns;			//earned runs
        	 //check if the sport is baseball and the position is pitcher do this
            if (pos.equalsIgnoreCase("Pitcher"))
            {
          	  atBats = in.nextInt();
          	  hits = in.nextInt();
          	  innings = in.nextDouble();
          	  earnedRuns = in.nextInt();
          	  Pitcher pit = new Pitcher(sport,team,pos,fn,ln,atBats,
          			  hits,innings,earnedRuns);
          	  theplayer.add(pit);
            }
            //check if the sport is baseball but the pos is not pitcher do this
            else if (!pos.equalsIgnoreCase("Pitcher"))
            {
          	  	atBats = in.nextInt();
          	  	hits = in.nextInt();
          	  	BaseballPlayer base = new BaseballPlayer(sport,team,pos,fn,
          			  ln,atBats,hits);
          	  	theplayer.add(base);
          	  	
            }
        }
        /**
         * this methods is used to stored an object when it is a soccer player
         * @param sport is the sport of the player in this case soccer
         * @param team is the team of the player 
         * @param pos is the position of the player
         * @param fn is the first name of the player
         * @param ln is the second name of the player
         */
        public void SoccerPlayer(String sport, String team, String pos,
        		String fn, String ln)
        {
        	int goalsScored;		//goals scores
            int goalsAllowed;		//goals allowed
            int minutes;			//minutes played
            
            if (pos.equalsIgnoreCase("goalie"))
            {
            	minutes = in.nextInt();
              	goalsScored = in.nextInt();
              	goalsAllowed = in.nextInt();
          		Goalie g = new Goalie(sport,team,pos,fn,ln,minutes,
          			goalsScored,goalsAllowed);
          		theplayer.add(g);
            }
            else if (!pos.equalsIgnoreCase("goalie"))
      		{
      			minutes = in.nextInt();
          		goalsScored = in.nextInt();
      			SoccerPlayer socc = new SoccerPlayer(sport,team,pos,fn,
      					ln,minutes,goalsScored);
          		theplayer.add(socc);
      		}
        }
        
        /**
         * read data and call a function that can be baseball or soccer
         */
        public void readData()
        {
        	//variable declaration/initialization
        	String sport, team, pos, fn, ln;	//strings variables
          
            //while loop that check the token until no more token are found
            while(in.hasNext())
            {
              sport = in.next();			//first token in the file: sport
              team = in.next();				//second token in the file: team
              pos = in.next();				//third token in the file: position
              fn = in.next();				//fourth token in the file: first name
              ln = in.next();				//fifth token in the file: last name
              
              if (sport.equalsIgnoreCase("baseball"))
              {
            	BaseballPlayer(sport,team,pos,fn,ln);
              }
              else if (sport.equalsIgnoreCase("soccer"))
              {
            	  SoccerPlayer(sport,team,pos,fn,ln);
              }
            }
        }
        
        /**
         * close the files that are been used in the program
         * the output file and the input file
         */
        public void closeFiles()
        {
        	in.close();
        	out.close();
        }
       
        /**
         * calculates the player with the highest batting average
         * @return the player with the highest batting average
         */
        public BaseballPlayer getHighBattingAverage()
        {
        	double max = 0;
        	BaseballPlayer base = new BaseballPlayer();
        	
        	//for each loop that check the array list named theplayer
        	for(Player p: theplayer)
        	{
        		//check if the object is an instance of baseballplayer
    		  if(p instanceof BaseballPlayer)
    		  {
    			  //if the player in the array list if higher than previous check
    			  if(((BaseballPlayer) p).battingAverage() > max)
    			  {
    				  max = ((BaseballPlayer) p).battingAverage();
    				  base = (BaseballPlayer) p;
    			  }
    		  }
        	}
    	  return base;
        }
      
        /**
         * get the loswest average goals allowed by a goalie
         * @return the lowest average goals allowed by a goalie
         */
        public Goalie getLowestAvgGoalsAllowed()
        {
        	//variable declaration/initialization
        	Goalie goal = new Goalie();
        	double max = 0;
    	  
        	//for each loop that check all the object in the array list theplayer
        	for(Player e: theplayer)
        	{
        		//get the first average goal allowed in array to use below and break
        		if(e instanceof Goalie)
        		{
        			max = ((Goalie) e).averageGoalsAllowed();
        			break;
        		}
        	}
        	
        	//for each loop that check all the object in the array list theplayer
        	for(Player p: theplayer)
        	{
        		//if it is an instance of goalie do this
        		if(p instanceof Goalie)
        		{
        			//if object average goal allowed is lower than previous check
        			if(((Goalie) p).averageGoalsAllowed() < max)
        			{
        				max = ((Goalie) p).averageGoalsAllowed();
        				goal = (Goalie) p;
        			}
        		}
        	}
        	return goal;
      }
      
        /**
         * output the arraylist name theplayer into a output file .txt
         */
        public void displayPlayers()
        {
        	//for each loop that check all the object in the array list theplayer
    	  	for(Player e: theplayer)
    	    {
    	  		//if object is an instance of baseball player do this
    	    	if(e instanceof BaseballPlayer)
    	    	{
    	    		out.println((BaseballPlayer)e);
    	    	}
    	    	//if object is an instance of soccer player do this
    	    	if (e instanceof SoccerPlayer)
    	    	{
    	    		out.println((SoccerPlayer)e);
    	    	}
    	    	
    	    }
      }
      
        /**
         * Main method
         */
        public static void main(String[] args) 
        {
            PlayersMain pm = new PlayersMain();
            pm.openFiles();      //opens the input and output files
            pm.readData();       //reads the data file and populates the arraylist
            BaseballPlayer b = pm.getHighBattingAverage();
            System.out.println(b);
            Goalie g = pm.getLowestAvgGoalsAllowed();
            System.out.println(g);
            pm.displayPlayers(); //output all players in the arraylist to the file
            pm.closeFiles();     //close input and output files
        }
    }

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,746
    Rep Power
    19

    Default Re: problem to open .txt with different types of token in each line

    That'll make that a lot easier to debug or change now.
    Also makes it (in my mind) easier to read...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 12-09-2011, 10:28 PM
  2. Replies: 2
    Last Post: 11-21-2011, 11:09 AM
  3. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  4. Replies: 0
    Last Post: 04-20-2011, 12:03 PM
  5. Open type (Ctrl-Shift-T) does not find types
    By Zhenya_Merom in forum Eclipse
    Replies: 2
    Last Post: 11-04-2009, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •