• Skip to main content
  • Skip to primary sidebar

Founder at work

Master the skills to create and launch your next SaaS company

  • About Me
  • Reviews
  • Resources
  • Community
  • Contact
  • Lessons
  • Series
  • Search
  • Login

How to readfiles in java?

November 21, 2012 by Rick Hernandez Leave a Comment

for this program i get an input from a file then i massage the data and write back out to a new file. IT worked good for having a file with data and changing it to represent something that is more usable.

package teamleader;

public class TeamLeader
{
    public static void main(String[] args)
    {
        //initialization
        int team;
        int totalEmployess = 0;
        int totalTeams = 0;
        int average;
        int index;
        int teamCount [] = new int [100];
        String PTL[] = new String[100];
        String employee;
        
        // in and out from files
        InputFile in = new InputFile("in.txt");
        OutputFile out = new OutputFile("out.txt");
        
        
        // for a loop that initilized the value of array
        for(index=1;index<8;index++)
        {
            teamCount[index] = 0;
            PTL[index] = "";
            index = index + 1;   
        } // end of the for loop
        
        System.out.println("Program Started...");
        
        while (!in.eof())
        {
            employee = in.readWord(); // reads employee
            team = in.readInt(); // reads Team
            
            teamCount[team] = teamCount[team] +1;
            
            if(teamCount[team] <= 3) //counts to see team
            {
                PTL[team] = employee;
            }// end of the if statement
            
            totalEmployess = totalEmployess +1;
            
        }// end of the while loop
        
        for(index=1;index<100;index++)
        {
            if (teamCount[index] > 0)
            {
                out.writeString("Team_Number:");
                out.writeInt(index);
                out.writeString("Team_Leader");
                out.writeWord(PTL[index]);
                out.writeString("Total_Members");
                out.writeInt(teamCount[index]);
                totalTeams = totalTeams + 1; 
            }// end of the if statment            
        } // end of the for loop
        
        out.close(); // closes the file
        
        average = AVG( totalEmployess, totalTeams );
        
        System.out.println(" Total Teams:" + totalTeams + " Average Team Size:" + average);
        
        System.out.println("Team Leader Completed Successfully. ");
        
       
        
    } // end of the main 
    
    
    // always set your functions outside the main 
     public static int AVG(int total, int divisor) // AVG Function 
        {
            int average = total/divisor;
            return average;
        }// end of the AVG function
} // end of team leader class

Rick Hernandez

I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Filed Under: Java, Programming

Primary Sidebar

Learn How To Take Your Idea And Convert It Into An App From Scratch Even If You Have Never Written A Single Line Of Code.

  • How to Find Your Next Great App Idea
  • The Secret To Getting PAID To Build Your Own App
  • How To Create Your App (SaaS, Mobile, VR, AR, Game)
  • How To Monetize Your Apps
Learn More
4.82 Ratings

Copyright © 2025 · JSecademy Blog Theme on Genesis Framework · WordPress · Log in