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
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.
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.. :rolleyes:
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" );
}
}
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.
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))
Quote:
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;
Quote:
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.