Results 1 to 6 of 6
  1. #1
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Count Certain token from text file

    Ok so I am supposed to make a program that reads employee info from a list of 5 employees contained in a file called employees.txt. All the names are laid out like "156387, Smith, Joe, G, m, 12/8/1969" and it is supposed to count how many male employees there are and how many females there are. I started writing a program and am trying to use tokens. I am trying to make some kind of if statement like "if(gender == m)" then it would increase the male counter "maleCounter++". I am having trouble with the "==" so that it can recognize m or f to see what gender the employee is. I am trying to convert it to a char. And I am also wondering if I need to use a loop with a "readline();" that reads each line individually. Sorry if that is confusing, I am even confused.

    Here is the problem:
    an employee data file, employees.txt, exists that stores the employee ID number (six digits), last name,
    first name, middle initial, gender (m or f), and a date-of-birth sting (mm/dd/yyy). Write a Java
    program called SortEmployees.java that reads the data from the file and outputs the number of males
    and females. Use the Scanner class for input. Test your program with a data file containing at least five
    employee records.

    Here is my current code:

    Java Code:
    import java.io.File;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    import java.io.BufferedReader;
    import java.io.FileReader;
    
    public class a
    	{
    	       public static void main( String [] args ) throws FileNotFoundException, throws 
    
    IOException
    	    {
        	  // declare and initialize variables
    
        	  String outputStr = "Average scores for students:\n\n";
    	  StringTokenizer tokenizer;
    	  int numTokens = 0;
    	  StringBuilder employeeSB = new StringBuilder();
    	  String[] empArray   = new String[6];
    	  String outputGender;
    	  int maleCount = 0 , femaleCount = 0;
    	  char m, f;
    
         	  File inEmployees  = new File( "employees.txt"  ); // File object
    	  FileReader aFileReader = new FileReader( inEmployees ); //pipeline
    	  BufferedReader aBufferedReader = new BufferedReader(aFileReader );
    	  String aRecord = aBufferedReader.readLine(); //open valve
    
    	  tokenizer = new StringTokenizer( aRecord, "," );
    	  numTokens = tokenizer.countTokens();
    
    	  for(int i = 0; i < numTokens; i++)
    	     empArray[i] = tokenizer.nextToken();
    
    	  employeeSB.append(empArray[4]);
    
    	  outputGender = employeeSB.toString();
    	  char gender = outputGender.charAt(0);
    
    	  if(gender == m)
    	  {
    		maleCount++;
    	  }
    
    	  if(gender == f)
    	  {
    		femaleCount++;
    	  }
    
    	System.out.println( "The number of Males is " + maleCount + ", the number of females is " + 
    
    femaleCount); 
    
    	    } //end main
    	} //end class
    Here is the text file:

    156387, Smith, Joe, G, m, 12/8/1969
    354862, Johnson, Bob, R, m, 08/12/1958
    324567, Buscuit, Frank, B, m, 02/02/1991
    365214, Green, Mary, T, f, 06/18/1986
    843156, Brown, Rob, Q, m, 05/16/1987

  2. #2
    jfabian is offline Member
    Join Date
    Jun 2012
    Posts
    22
    Rep Power
    0

    Default Re: Count Certain token from text file

    Line 40

    Java Code:
          if(gender == m)
    char values are enclosed within single quotation marks, so it should be:
    Java Code:
          if(gender == 'm')
    Line 45 also have this mistake.

  3. #3
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Count Certain token from text file

    Another issue is at Line 37 and 38
    Java Code:
    outputGender = employeeSB.toString();
    char gender = outputGender.charAt(0);
    there is a space in outputGender that looks like " m". To get by this just add the line
    Java Code:
    outputGender = employeeSB.toString();
    outputGender = outputGender.replace(" ","");
    char gender = outputGender.charAt(0);
    which will fix that

    Also the program only runs through once. you will need some find of loop that will iterate through the file list

  4. #4
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Count Certain token from text file

    Just a tip.
    From what I have learned and re-researched from the Java API documentation, StringTokenizer should not be used and should be replaced with String .split() method. It is based on Regex (regular expressions). If you are uncomfortable with regex like I am, you can keep using StringTokenizer, but there are alternatives that, at least the Java API, says are better choices.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  5. #5
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Count Certain token from text file

    Ok so I changed it all up and here is the new code. It works except it doesn't print the output line. And how can I remove the info contain in the text file from the output? Thanks in advance!

    Java Code:
    import java.io.*;
    
    public class Employees
    {
    	public static void main(String args[]) throws IOException
    	{
    		File f=new File("employees.txt");
    		FileInputStream fi=new FileInputStream(f);
    		int i=fi.read();
    		int index=-1;int newline[]=new int[1000], index1=0;
                    int male=0;int female=0;
    		char c[]=new char[5000];
    		while(i!=-1)
    		{index++;
    			c[index]=(char)i;
    			System.out.print((char) i);
    				if((char)i=='\n')
    				{
    					newline[index1]=index;
                                            index1++;
    				}
    				i=fi.read();
    		}
                    for(int i1=0;i1<=index;i1++)
                    {
                        if(c[(newline[i1]-1)]=='m')
                        {
                            male++;
                        }
                        else
                        {
                            female++;
                                    
                        }
                                
                            
                    }
                    
                    System.out.print("Total number of male and female are"+male+" "+female);
    }
    }
    This is the employees.txt file:
    Id LastName FirstName Middle DOB Gender
    123456 Green James R 10/10/1979 m
    234589 Brown Mary T 09/09/1980 f
    345778 Wood Lisa F 04/04/1986 f
    567890 Johnson Tom H 02/02/1986 m
    123589 Wallace Josh B 01/01/1978 m


    Here is the text from command prompt:

    Id LastName FirstName Middle DOB Gender
    123456 Green James R 10/10/1979 m
    234589 Brown Mary T 09/09/1980 f
    345778 Wood Lisa F 04/04/1986 f
    567890 Johnson Tom H 02/02/1986 m
    123589 Wallace Josh B 01/01/1978 m
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at SortEmployees.main(SortEmployees.java:26)

  6. #6
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Count Certain token from text file

    I dont know if you really want to go this way. I believe its reading out char by char. A more efficient way would be to use BufferedReader and read it out line by line in a while loop.
    But if you want to go this route the reason you are getting the ArrayIndexOutOfBoundsException: is this loop
    Java Code:
            for(int i1=0;i1<newline.length-1;i1++)
    newline array is 1000 chars long because that's what you set it to at the beginning. So you would need to adjust that part

Similar Threads

  1. Replies: 2
    Last Post: 11-21-2011, 11:09 AM
  2. Read text file and count words.
    By chdn202002 in forum New To Java
    Replies: 5
    Last Post: 07-17-2011, 02:44 PM
  3. Replies: 3
    Last Post: 11-09-2010, 12:10 AM
  4. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  5. Replies: 3
    Last Post: 03-22-2010, 07:27 PM

Posting Permissions

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