This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello, JavaRanch. My name's Aaron, and as this thread's title states, a newbie to the Java world. I understand a little about Java after 9 weeks of classes/labs, even more since I have a few friends who can help. I'm here today to ask for assistance regarding a code I'm working on now. The situation is, I need to read data from an external text file (which I've already completed), break the contents into tokens with StringTokenizer (also completed), then output the data in the format:
Students with grades of X between Y:
LastName FirstName Grade
LastName FirstName Grade
... and so on
I need to do this for the following ranges; 69 and below, 70 to 79, 80 to 89, and 90 to 100 (I've also done this). But the problem is, the output is all screwed up. Here's the code:
The output turns out like this:
See what I mean? What I'm trying to do is get it so each person is under the correct range without the range showing up so many times. I also need to output the names of people who don't have any grades available, but those turn out as Exceptions. Can anyone give me a hand here? Thanks in advance.
The problem with your output is you are printing the title Students with grades between X and Y for each row you read in from the file.
Now I’m not the smartest algorithmic programmer but I would do something like this.
Make an inner class called student
Then as you read in each line create a Student object, using your current if statement (which could be supplemented with a switch statement), place the new Student object into a Map, that holds a list of students under a specific key. So the key in the map for each Student object list will be your title “Students with grades between X and Y”, get the list belonging with that key and place the student into the list. So you would have 4 entries in the map, corresponding to a List holding the students that fall into that particular grade category.
Once you’ve read in all the students and placed them into their corresponding list assigned to the key. Iterate through the map keys and for each key iterate through the list of students. At the same time totaling up ALL the grades to get the grade average.
Which then should give you your desired output. Sorry about the brief explanation but I’m at work and can’t do a detailed example, but hopefully you get what I’m trying to say.
As for Exception handling, I don't have much time to go into ideas for that. Sorry.
First thing to realize: you are going to have to read all your students into some kind of collection before you start your output. You cannot read one student at a time and output their info if you want to collect students by grade range.
You can declare an "inner class" as specified, or put that in its own file named Student.java. You are going to need some way to store a name (or two) and a grade for each student.
The other poster is correct; store the name (in one or two variables) and the grade in the Student class. You can plan to create a student object with a constructor that takes these items, and a pair of methods to get the values out of the object. This is a complete Student class; the fact that mine is declared "public" class means it has to be in its own file. If you declare it as the other poster suggested, you can declare it in this file.
and then in your code:
Now, there are multiple things about this code that can be improved and teach you more about writing good code -- it's a good exercise -- but rather than try to do all of those at once, I think this may be enough information for you to go ahead and finish this task the way you wanted to.
Let us know.
Joined: Mar 31, 2011
Yeah, sorry about that everyone. I had a few problems that had to be dealt with. As far as the code goes, I've revised it. As far as Maps and Lists go, I'm afraid that's beyond my level of skill.. I had a little help from a friend, who told me to make strings for each range and my code came out like this:
Now, there's one last problem. A new addition to this assignment is, if I understood it right, to output the data I get from running the program to a new file, like "GPA2.txt". I believe my professor wants me to use PrintWriter, but I'm not entire sure I understand how it works. I know the constructor for it:
But I don't understand how the parameters work here. Once I figure this out, get in my code, and print my output to a new file, I'll be officially done. So I must again ask for your assistance here, everyone. ^_^;