Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Write a Java program that reads file

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Thanked 0 Times in 0 Posts

    Default 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 but not im stuck... HELP!

     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
    Jul 2012
    My Mood
    Thanked 6 Times in 6 Posts

    Default Re: Write a Java program that reads file

    You are not stuck, thats a good start.
    What do you need help with? other then writing a code for you.

  3. #3
    Join Date
    Jun 2012
    Left Coast, USA
    My Mood
    Thanked 97 Times in 88 Posts

    Default Re: Write a Java program that reads file

    Quote Originally Posted by Mehwish-S View Post
    ...but not im stuck... ..
    I am guessing that you meant, "now I'm stuck."


    If you have some part of the program working and it doesn't give the output you expect, then one approach to debugging involves putting print statements at strategic points in the program.

    When you read a line, print the line.
    When you split a string, print all of the resulting strings.

    Stuff like that.

    Now, if you get to a point where you simply don't understand the way it works (or doesn't work), then here are my suggestions:

    1. Post a small input file (a couple of lines will be enough to show us the format).

    2. Post your code that shows where you put the print statements.

    3. Post the program output. (Paste the output into your post. Don't just tell us that you "got the wrong answer." Show us.)

    4. Tell us what you expected to happen.

    5. Tell us what, exactly, you don't understand about the difference between what you expected and what you got.


    Last edited by Zaphod_b; July 28th, 2012 at 03:09 PM.

Similar Threads

  1. Replies: 1
    Last Post: May 2nd, 2012, 08:06 AM
  2. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM
  3. Creating a program that reads from a file
    By Twoacross in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: November 20th, 2011, 10:19 PM
  4. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  5. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM

Tags for this Thread