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 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    file handling, copying sections of one file into another file

    Hi, I am a beginner and I have worked on this code for a while. I am stuck, I can't get the code to copy the sections of the file student.txt to another file. I also need to work out how to find out the average of the students assignment results. I have included the code which I have been working on which you can compile to see the fault and I have included the student.txt file.


    import java.util.Scanner;
    import java.io.*;
    import java.io.IOException;
    import java.io.FileNotFoundException;


    /*
    This program is to read the student.txt file and copy the student number and all the students
    assignment results to another file. If the file is not found or their is a numberFormatException
    then the programme will throw this exception.

    Code:
    */
    
        public class FileOnStudents2
       {
           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 = 0;
                String studentFirstName;
                String studentLastName;
                String stringResults = "87.4";
                double resultsValue;
                String gender;
             	
             	
                String fileName = "students.txt";
                FileReader reader = new FileReader(fileName);
                Scanner in = new Scanner(reader);
                //studentNumber = Integer.parseInt(input);
                PrintWriter out = new PrintWriter(outputFileName);
             
             	
                try{ 
                
                   String line = in.nextLine();
                   out.println( line);
                	//String line = in.nextLine();
                   //out.println( line);
                
                      
                  //  Scanner lineScanner = new Scanner(line);
                //                   studentStringNumber = lineScanner.next();
                //                   while(!lineScanner.hasNextDouble())
                //                   {
                //                      studentStringNumber = studentStringNumber+ " " +lineScanner.next();
                // 						}                  }
                //                   double resultsValue = lineScanner.nextDouble();                             
                      // String line = in.nextLine();
                //                   out.println( line);
                //                           
                   int i=0;
                   if(!Character.isWhitespace(line.charAt(i)))  { i++; }
                   studentStringNumber = line.substring(0,i);
                   String stringResult = line.substring(i);
                   studentStringNumber = studentStringNumber.trim();
                   resultsValue = Double.parseDouble(stringResults.trim());
                   stringResults = stringResults.trim();
                   resultsValue = Double.parseDouble(stringResults.trim());
                   stringResults = stringResults.trim();
                   resultsValue = Double.parseDouble(stringResults.trim());
                               
                   stringResults = in.nextLine();
                                              	
                                                 
                   studentStringNumber = in.nextLine();
                   studentNumberValue = Integer.parseInt(studentStringNumber);
                   if(in.hasNextInt())
                   {
                      int studenNumberValue = in.nextInt();
                      in.nextLine(); //consume the newline
                      stringResult = in.nextLine();
                   }
                               						 					   
                   //results = Double.parseDouble(stringResults); 
                   if(in.hasNextDouble())
                   {
                      double resultValue = in.nextDouble();
    						in.nextLine();
    					   stringResult = in.nextLine();
                   } 
    					 if(in.hasNextDouble())
                   {
                      double resultValue = in.nextDouble();
    						in.nextLine();
    					   stringResult = in.nextLine();
                   } 
    
                               	
                   Scanner lineScanner = new Scanner(line);
                   studentStringNumber = lineScanner.next();
                               	
                }
                       
                finally{
                   reader.close();
                   out.close();
                   in.close();
                }
             }
                 catch(IOException exception)
                {
                   exception.printStackTrace();
                }
                 catch (NumberFormatException exception)
                { 
                   System.out.println("Result should be a double:" + exception);
                }
          		
          }
       }
    student.txt file:-
    L00076321 Kathleen Sherry 87.4 78.5 34.6 Female
    L00087654 Veronica Brown 67.9 54.6 87.5 female
    L00045781 Mary Davis 45.9 78.6 56.8 Female
    L00065782 Thomas Scullion 76.9 87.5 56.4 Male
    L00056431 James Rooney 76.9 45.8 98.3 Male


    student.txt file:-

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    This is a repeat of your previous forum post ..... Probably shouldnt have done this...

  • Users who have thanked renegadeandy for this post:

    kitty (12-10-2010)

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I am not sure of how a forum works, but I am a bit anxious to get this assignment done and I am finding that I am stuck. Can you kindly look at the code and see where it is wrong and why it is not taking the sections that I need. Thanks ever so kindly for replying to me.

  • #4
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Hmm right - probably gona struggle coding in Java if you dont understand how a forum works!

    Regardless ive taken a look at your problem and solved it.

    You were making the problem more difficult than it needed to be - so now the program does this:

    For each line in the file
    Read line
    split the line on a space, so we can look at each word in the line
    then split the words into the appropriate variables
    get the 3 results add them up and divide by 3 to get the average
    write the line
    move onto next line

    And its that simple!

    Code is :

    Code:
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    
     public class FileOnStudents2
       {
           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 = 0;
                String studentFirstName;
                String studentLastName;
                String stringResults = "87.4";
                double resultsValue;
                String gender;
             	
             	
                String fileName = "students.txt";
                FileReader reader = new FileReader(fileName);
                Scanner in = new Scanner(reader);
                //studentNumber = Integer.parseInt(input);
                PrintWriter out = new PrintWriter(outputFileName);
             
             	
                try{ 
                while(in.hasNext()){
                   String line = in.nextLine();
                   System.out.println("Processing line:"+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 val1 = Double.parseDouble(sections[3]);
                   double val2 = Double.parseDouble(sections[4]);
                   double val3 = Double.parseDouble(sections[5]);
                   resultsValue  = (val1 + val2 + val3) / 3;
                   gender = sections[6];
                               
                out.write(studentStringNumber + " " + studentNumberValue +" "+studentFirstName + " "+ studentLastName + " "+ stringResults + " "+ resultsValue+ " "+ gender+"\n");
                
     }
                
                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);
                }
          		
             
          
                
                 }
       }
    Hope this helps and you learn well from it.

  • Users who have thanked renegadeandy for this post:

    kitty (12-14-2010)

  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile

    thanks for so much. It is really appreciated. Sorry for not thanking you sooner, but I took sick and only now recovering. I do want to get this qualification, however I think you might be right and I am best to avoid programming as a job. I do tend to complicate it and that is where my problem lies. Thanks again, one ever so grateful forum user.

  • #6
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    Thats not a problem.

    You dont need to avoid doing programming as a job. Its just a learning curve and you've gone further than a lot of other people would.

    There are lots of easy ways of doing stuff - its just being able to find it which is what these forums are for


  •  

    Posting Permissions

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