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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 5 of 5

Thread: Reading File and Outputting Data

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Reading File and Outputting Data

    I am a new user here in the Java Forums. I am actually a student at Trine University. I am not searching for any exact code answers, but would appreciate any help on this. I feel we haven't touched on this subject enough in order to write this program, so any help would be greatly appreciated. The program is posted below. I have created the text file and placed it in the correct location and declared it using java.io.File. What other packages should I need to import for this project?
    :::::

    Write a program that reads all records from a file named input.txt in the following format:

    studentId studentName gpa

    studentId studentName gpa

    (studentid is integer, studentName is a two word string and gpa is a double value).

    The program should count the number of records read from the file and the average gpa of the class. The program should also count how many students have gpa greater than 3.0. Output all the information in proper format (use DecimalFormat class). Follow the example 6.2 from the textbook to read data from file. Do not make any assumptions about the file other than what is mentioned above.

    The file input.txt has to be located in your project folder under Netbeans Projects, so if your project name is Homework5 then file input.txt should be in the folder (Netbeans Projects\Homework5), not inside build directory.

    Extra Credit:Find and print the max gpa score

    Thanks for any help guys,

    -Chris Messersmith


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Reading File and Outputting Data

    Posting your exercise specifications and saying HELP isn't going to get you constructive replies, let alone code snippets.
    We here at Java Programming Forums are here to help you learn, not to spoon-feed you answers.

    To increase your chances of being helped, you need to show good faith, start writing the project and see how far you can get.
    Once you hit a brick wall, you can come back and ask specific and direct questions regarding what errors you're getting and / or what concepts you can't get your head around.

    If you can do that, you really will be helping yourself much more than you think.
    Last edited by newbie; November 13th, 2011 at 07:58 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading File and Outputting Data

    Please do not criticize me when I specifically stated that I DO NOT want specific code answers. You might want to re-read my post. I HAVE hit a brick wall because I don't believe we have gone over the subject very clearly. I just don't understand how to get the program to read from a text file. So reconsider what you're saying before you reply with criticism. Just because I am a new user does not mean I came here to get snippets of code "spoon-fed" to me. I am obviously wanting to learn this subject, and believe it was necessary for me to ask for help here. You're a forum VIP and you're criticizing a new user for what reason? I came to learn, not get answers given to me. Thanks for your constructive feedback though..

    Oh, and don't think that I came here before trying to figure this out myself, because I have been trying to figure it out for multiple hours through research and errors.

    To anyone else who might be willing to help, I have posted my current code below, and there are no errors given. I am absolutely clueless to what comes next. Maybe a try/catch?

    /*==========================================
    * Author: Chris Messersmith
    * Class: CS1113: Homework 5
    * Purpose: Read a text file and calculate.
    ===========================================*/
    package homework5;

    import java.io.File;
    import java.util.Scanner;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.*;


    public class Homework5 {



    public static void main(String[] args) throws IOException {


    //Reading text file input.txt
    File textFile = new File( "input.txt" );





    }
    }
    Last edited by ChrisMessersmith; November 13th, 2011 at 11:20 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Reading File and Outputting Data

    What is the code supposed to do after it defines a File object?
    For reading simple files, the Scanner class may be useful.

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Reading File and Outputting Data

    You said you weren't looking for exact code answers.
    You then proceeded to say you hadn't been taught enough of Java to accomplish your task - which I can't argue with.

    Then your first question was along the lines of, "I've imported java.io.File, but now what other packages would I need?",
    which is a question, but a fairly subjective one as there are many approaches one could take to accomplish a task.

    If you had posted the code you had, as little as It may be... I wouldn't have pounced so quickly, I would have just given you a link to the Java tutorials
    (Found here by the way: File I/O (Featuring NIO.2) (The Java™ Tutorials > Essential Classes > Basic I/O))

    You might want to re-read my post. I HAVE hit a brick wall because I don't believe we have gone over the subject very clearly
    That is not specifically a Java related issue that WE can deal with though, It's your problem which you need to go away and deal with; i.e. read the tutorials.
    Anyway, I'm just a casual contributor and It is only my opinion... don't be put off the forums on my account but clear and concise
    questions will get you more replies / help.


    Back to the matter at hand;
    To anyone else who might be willing to help, I have posted my current code below, and there are no errors given. I am absolutely clueless to what comes next. Maybe a try/catch?
    As you're new to this, lay off Exception handling for a while and just get the logic in.
    Start by writing a Student class, which represents a student's attributes, then you can possibly store your read in data into an ArrayList of Students from where you can easily and repeatedly access the data (After reading the tutorials of-course).

    - Obviously the way you approach it may vary.
    Last edited by newbie; November 13th, 2011 at 01:42 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Help with Scanner class - Reading Data from a file
    By billias in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 12:07 PM
  2. error related arrays outputting wront data...
    By semicolon in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 1st, 2011, 09:34 AM
  3. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 09:13 AM
  4. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM
  5. Reading file Data into a "struct"
    By Brandt in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 9th, 2011, 10:02 AM