CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2012
    Posts
    4

    Write a Java program that reads file

    Write a Java program that reads the file "X", and prints to the console the file update time, the list of marks for a given UB number and the corresponding “marks gained” value.
    So far I have got this and not im totally stuck :/
    Help!

    Code:
     package textcopy;
    import java.io.*;
    /**
     * This class demonstrates the number of students matching the UB number criterion and the average mark.
     *
     * @author  MSheraz3
     */
    public class TextCopy {
        public static void main(String[] args) throws IOException {
            
            BufferedReader reader = new BufferedReader(new FileReader(args[0]));
            BufferedWriter writer = new BufferedWriter(new FileWriter(args[1]));
            
                String line = null; 
                    while ((line=reader.readLine()) != null)
                    if ( line.split(",")[0].matches("\\d*7\\d*") )
                    { writer.write(line); writer.newLine(); }
                    reader.close(); writer.close(); 
            
            double sum=0; int counter=0;
            { sum = sum+Integer.parseInt(line.substring(9)); counter = counter+1; }
            
            System.out.println("Students: "+counter+"\nAverage mark: "+sum/counter);
        }
    }

  2. #2
    Join Date
    Nov 2011
    Location
    Vietnam
    Posts
    8

    Re: Write a Java program that reads file

    What are you stuck with? Please be more specific so others can help you quickly.

  3. #3
    Join Date
    Jul 2012
    Posts
    4

    Re: Write a Java program that reads file

    Quote Originally Posted by nambill View Post
    What are you stuck with? Please be more specific so others can help you quickly.
    Can you please show me your way of how you would write this code & then i will alter mine according to what i need.
    Thank you

  4. #4
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Write a Java program that reads file


  5. #5
    Join Date
    Aug 2012
    Posts
    2

    Re: Write a Java program that reads file

    Code:
    		public static void Save() {
    			File file = new File("Save.txt"); //Output files name
    			
    			try {
    				
    			Writer output = null;
    			output = new BufferedWriter(new FileWriter(file));
    			output.write(""); //Writes the File
    			output.close(); //Closes the output
    			//System.out.println("");
    			
    			}  catch(IOException e) {
    				e.printStackTrace();
    			}
    		}
    		public static void Read() {
    			try {
    				File file = new File("Save.txt");
    				BufferedReader reader = new BufferedReader(new FileReader(file));
    				String line = "";
    				while ((line = reader.readLine()) != null) {
    					//System.out.println(line);
    				}
    				reader.close();
    				reader = null;
    			} catch (FileNotFoundException e) {
    				//The error should not show up, as of if the file doesn't exist, the void should just ignore it.
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    Should work..

  6. #6
    Join Date
    Jul 2012
    Posts
    4

    Re: Write a Java program that reads file

    Thank you so much! had to do some alteration but its perfect now
    Thanks again!

  7. #7
    Join Date
    Aug 2012
    Posts
    2

    Re: Write a Java program that reads file

    Quote Originally Posted by Mehwish-S View Post
    Thank you so much! had to do some alteration but its perfect now
    Thanks again!
    No problem sir, anytime.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured