Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    file-handling, creating two files from a single file

    Hi I am a beginner and am struggling to find anything which will show me how to separate a file into two files with one file males and the other females. Can someone help
    Code:
    import java.io.FileNotFoundException;
       import java.io.FileReader;
       import java.io.IOException;
       import java.io.PrintWriter;
       import java.util.Scanner;
     
       public class GenderMaleAndFemaleFiles
       {
          public static void main(String[] args) throws FileNotFoundException
          {
          
             Scanner keyIn = new Scanner(System.in);
              
             System.out.print("Output file: ");
             String outputFileName = keyIn.next();
          	      
          		
          
             try
             { 
             
             	
                //Scanner in = new Scanner(new File(outputFileName));
                String studentStringNumber= "L";
                int studentNumberValue = 1;
                String studentFirstName = "Kathleen";
                String studentLastName = "Sherry";
                String stringResults = "87.4";
                double resultsValue = 87.4;
                String gender1 = "Female";
                String gender2 = "Male";
             	
             	
                String fileName = "students.txt";
                FileReader reader = new FileReader(fileName);
                Scanner in = new Scanner(reader);
                PrintWriter out = new PrintWriter(outputFileName);
             	
             		
                try{ 
                   while(in.hasNext()){
                      String line = in.nextLine();
                      System.out.println(" " +line);
                   
                                     
                      String[] sections = line.split(" ");
                   	
                      studentFirstName = sections[1];
                      studentLastName = sections[2];
                      gender1 = sections[6];
    						gender2 = sections[6];
                   	
                      if(gender1.equalsIgnoreCase("Male"));
                      {
                      
                         out.write(studentStringNumber + " " + studentNumberValue +" " 
    							+studentFirstName+ " " +studentLastName+ ""+ stringResults + " "+ resultsValue+ " "
    							+gender1);
                      }
                   }
                	            
                	System.out.print("Output file: ");
                   outputFileName = keyIn.next();
                
                
                
                		gender ="Male";               
    						if(in.hasNext())
    						{
                      String line = in.nextLine();
                      System.out.println(" " +line);
                                  
                      if(gender.equalsIgnoreCase("Female"))
                      {               
                      
                           out.write(studentStringNumber + " " + studentNumberValue +" " 
    							+studentFirstName+ " " +studentLastName+ ""+ stringResults + " "+ resultsValue+ " "
    							+gender1)                             
                   }
                
                	
                }
                finally{
                   reader.close();
                   out.flush();
                   out.close();
                   in.close();
                }
             }
                catch(IOException exception)
                {
                   exception.printStackTrace();
                }
                catch (NumberFormatException exception)
                { 
                   System.out.println("Result should be a double:" + exception);
                }
                    	
          }
       }
    student file which has to be divided into two files one female and the other male:-
    L00076321 Kathleen Sherry 87.4 78.5 34.6 Female
    L00087654 Veronica Brown 67.9 54.6 87.5 Female
    L00076431 Mary Davis 45.6 67.9 89.5 Female
    L00065782 Thomas Scullion 76.9 87.5 56.4 Male
    L00056431 James Rooney 76.9 45.8 98.3 Male

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    I gave you an entire solution to this problem about 4 days ago...What was wrong with that one - this appears to be that solution copied + kinda broken again.... You are going to need to explain what you changed, why you changed it and why its not working.

    Come on... i spent a good 20 minutes or so writing a working application to do exactly what you wanted - then you come back a couple of days later posting a broken copied version!

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    hi, thanks for your reply. Do not be too upset, the code you kindly wrote for me is being used for that specific file handling section of my assignment. The code in this thread is the code I have attempted to adapt to work out how to write the code you gave me and adapt it so that it will print two files one on female.txt and males.txt from the file student.txt. I don't have a scooby how to do this and the lecturer doesn't seem to have given any instruction on how this is done. Can you kindly use your knowledge to help me once more and then that will be the assignment finished? It was meant to be handed in over a week ago. I hope this explanation helps to alleviate your annoyance and I am very appreciative of what you have done for me already.

  • #4
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Here you need to at least try the code:

    Take my code from before:

    Find the token for male / female probably something like

    String gender = something[x];

    if(gender.equals("Male"){
    //write to a male.txt ouput stream
    }
    else if(gender.equals("Female"){
    //write to a female.txt output stream
    }

    If you do that at the end just before the write statement - its a tiny little 5 minute change + you have what you want.

    Try it, paste the code here if you get stuck + i shall show you the rest if you get stuck.

    Andy

  • Users who have thanked renegadeandy for this post:

    kitty (12-17-2010)

  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for this, I will work on it today. Cheers from a fellow Scot.

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I have worked at trying to put what ever it is for String gender = something[];
    It looks like it could be a smaller array where the genders are broken down, but I could not get this to work either. I don't have the experience and knowledge to do this. I bet it is something very simple too. I did manage to get the programme to write out two files, which is progress. If you could show me the last wee bit that would be great. You know what they say, that life is a learning curve and with your help I am learning a lot about java. Thanks again Andy.



    Code:
    import java.io.FileNotFoundException;
       import java.io.FileReader;
       import java.io.IOException;
       import java.io.PrintWriter;
       import java.util.Scanner;
    
       public class GenderSortFile
       {
          public static void main(String[] args) throws FileNotFoundException
          {
          
             Scanner keyIn = new Scanner(System.in);
              
             System.out.print("Output file1: ");
             String outputFileName1 = keyIn.next();
          	
             System.out.print("Output file2: ");
             String outputFileName2 = keyIn.next();
          
          		
          
          
          		
          
             try
             { 
             
             	
                //Scanner in = new Scanner(new File(outputFileName));
                String studentStringNumber= "L";
                int studentNumberValue = 1;
                String studentFirstName;
                String studentLastName;
                String stringResults = "87.4";
                double resultsValue;
                String gender;
                String female;
                String male;
             	
             	
                String fileName = "students.txt";
                FileReader reader = new FileReader(fileName);
                Scanner in = new Scanner(reader);
                PrintWriter out1 = new PrintWriter(outputFileName1);
                PrintWriter out2 = new PrintWriter(outputFileName2);
             
             
             	
                try{ 
                   while(in.hasNext()){
                      String line = in.nextLine();
                      System.out.println(" " +line);
                   
                   
                      String[] sections = line.split(" ");
                   
                      studentStringNumber= sections[0];
                      studentNumberValue = Integer.parseInt(studentStringNumber.substring(1, studentStringNumber.length()));
                      studentFirstName = sections[1];
                      studentLastName = sections[2];
                      stringResults = sections[3]+" " + sections[4]+" "+ sections[5];
                      double value1 = Double.parseDouble(sections[3]);
                      double value2 = Double.parseDouble(sections[4]);
                      double value3 = Double.parseDouble(sections[5]);
                      resultsValue  = (value1 + value2 + value3) / 3;
                      gender= sections[6];
                   							
                                  	
                      gender = "Female";
                      gender= "Male";					  
                   	
                      if(gender.equals("Female"))
                      {
                         out1.write(studentStringNumber + " " + studentNumberValue +" "+studentFirstName + " "+ studentLastName + " "+ stringResults + " "+ resultsValue+ " "+ gender+"\n");
                      }
                      else if(gender.equals("Male"))
                      {
                         out2.write(studentStringNumber + " " + studentNumberValue +" "+studentFirstName + " "+ studentLastName + " "+ stringResults + " "+ resultsValue+ " "+ gender+"\n");               
                      }
                   }
                }
                finally{
                   reader.close();
                   out1.flush();
                   out2.flush();
                
                   out1.close();
                   out2.close();
                
                   in.close();
                }
             }
                catch(IOException exception)
                {
                   exception.printStackTrace();
                }
                catch (NumberFormatException exception)
                { 
                   System.out.println("Result should be a double:" + exception);
                }
                    	
          }
       }

  • #7
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Alright ill take a look later on this evening - but perhaps you should do some proper training 1 on 1 tutoring if you need such help! What part of Scotland you from?

  • #8
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Alright you made a good effort on that one actually. You were quite close!

    The if statements were good - except you wanted :

    if male then output using a male.out else if female then output using a female.out both ifs inside your while loop.

    On each line you read from students.txt - you grab the data - then check if its a male or female and output to the new file as appropriate.

    Anyway - the program when it runs now asks for the male filename, then female filename before creating the appropriate files correctly as follows:

    Code:
    
    Output file for male entries: males.txt
    Output file for female entries: females.txt
     L00076321 Kathleen Sherry 87.4 78.5 34.6 Female
     L00087654 Veronica Brown 67.9 54.6 87.5 Female
     L00076431 Mary Davis 45.6 67.9 89.5 Female
     L00065782 Thomas Scullion 76.9 87.5 56.4 Male
     L00056431 James Rooney 76.9 45.8 98.3 Male
    That would create 2 files a males.txt with :

    Code:
    L 1 Thomas Scullion87.4 87.4 Male
    L 1 James Rooney87.4 87.4 Male
    and a females.txt with :

    Code:
    L 1 Kathleen Sherry87.4 87.4 Female
    L 1 Veronica Brown87.4 87.4 Female
    L 1 Mary Davis87.4 87.4 Female
    Im not sure if you got the L bit right - isnt that meant to be the entire Lxxxxxxxx bit from the students.txt file? Ill let you fix as appropriate!

    The entire code to do that is as follows:

    Code:
    import java.io.FileNotFoundException;
       import java.io.FileReader;
       import java.io.IOException;
       import java.io.PrintWriter;
       import java.util.Scanner;
     
       public class GenderMaleAndFemaleFiles
       {
          public static void main(String[] args) throws FileNotFoundException
          {
          
             Scanner keyIn = new Scanner(System.in);
              
             System.out.print("Output file for male entries: ");
             String outputFileNameMale = keyIn.next();
          	      
             System.out.print("Output file for female entries: ");
             String outputFileNameFemale = keyIn.next();	
          
             try
             { 
             
             	
                //Scanner in = new Scanner(new File(outputFileName));
                String studentStringNumber= "L";
                int studentNumberValue = 1;
                String studentFirstName = "Kathleen";
                String studentLastName = "Sherry";
                String stringResults = "87.4";
                double resultsValue = 87.4;
                String gender1 = "Female";
                String gender2 = "Male";
             	
             	
                String fileName = "C:\\Documents and Settings\\Administrator\\IBM\\RDZ\\Test\\src\\students.txt";
                FileReader reader = new FileReader(fileName);
                Scanner in = new Scanner(reader);
                PrintWriter outMale = new PrintWriter(outputFileNameMale);
                PrintWriter outFemale = new PrintWriter(outputFileNameFemale);
             		
                try{ 
                   while(in.hasNext()){
                      String line = in.nextLine();
                      System.out.println(" " +line);
                   
                                     
                      String[] sections = line.split(" ");
                   	
                      studentFirstName = sections[1];
                      studentLastName = sections[2];
                      gender1 = sections[6];
    					
                   	
                      if(gender1.equalsIgnoreCase("Male"))
                      {
                      
                         outMale.write(studentStringNumber + " " + studentNumberValue +" " 
    							+studentFirstName+ " " +studentLastName+ ""+ stringResults + " "+ resultsValue+ " "
    							+gender1+"\n");
                      }
                      
                      else if(gender1.equalsIgnoreCase("Female"))
                      {               
                      
                           outFemale.write(studentStringNumber + " " + studentNumberValue +" " 
    							+studentFirstName+ " " +studentLastName+ ""+ stringResults + " "+ resultsValue+ " "
    							+gender1+"\n");                             
                   }
                      
                   }
                	            
               
                
                   
    					
                     
                
                	
                }
                finally{
                   reader.close();
                   outMale.flush();
                   outFemale.flush();
                   outMale.close();
                   outFemale.close();
                   in.close();
                }
             }
                catch(IOException exception)
                {
                   exception.printStackTrace();
                }
                catch (NumberFormatException exception)
                { 
                   System.out.println("Result should be a double:" + exception);
                }
                    	
          }
       }
    What is this work for? Is it for a University course. Have you thought about tuition?

  • #9
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I am a bankie from Clydebank. And this assignment is for a post grad higher diploma course in the South of Ireland. I am just about to complete the course with your help. It is the first time I have asked for help in the course all the other programming I did myself - I have to admit it was a struggle. thanks for all your help and maybe I will go on and pursue other courses which will need the assistance of a very proficient programmer like you. Thanks again - one very pleased coder.


  •  

    Posting Permissions

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