Computer Science Canada Another Program Problem |
Author: | Caceres [ Mon Jul 17, 2006 4:52 pm ] | ||
Post subject: | Another Program Problem | ||
Hi my goal is: Create a program that will read a file called "stds.txt". (The file stds.txt is in the view/complete button below). The data is in the format name-mark-mark-mark-mark. Download the file so that you can test your program. The last record has an entry xxx 0 0 0 0. With the data on each student, output the students name, their four marks, and the average of their four marks The file stds.txt looks like this: Quote: Smith 56 65 52 62
Jones 87 65 84 76 Ricci 34 52 45 55 Lombardi 76 77 50 78 Santos 56 77 78 68 xxx 0 0 0 0 The code i have so far is:
I keep on getting an error..It shows this... http://www.maj.com/gallery/galeontiger/GFX/error.jpg |
Author: | Caceres [ Mon Jul 17, 2006 4:53 pm ] |
Post subject: | |
The problem is that i was wodnering if there's a way to choose the next line. Like the data from the next line. I have smith's line, but I want to go to the next one. Thanks. ~Caceres |
Author: | wtd [ Mon Jul 17, 2006 5:19 pm ] |
Post subject: | |
Are you required to use classes that are not in the standard Java library? |
Author: | Caceres [ Mon Jul 17, 2006 5:24 pm ] |
Post subject: | |
wtd wrote: Are you required to use classes that are not in the standard Java library?
I'm not sure what that question means exaclt.y But we're only allows to use import TerminalIO.KeyboardReader; import becker.io.*; import java.io.*; The smith lines works perfecltly, with the marks and everything. But then i want to read the next line from the file, and I can't seem to get it. :S It stops after smith and says "next line is not available" or something like that. ![]() |
Author: | rizzix [ Mon Jul 17, 2006 5:28 pm ] | ||
Post subject: | |||
Here's an example using java 1.5's Scanner class..
|
Author: | wtd [ Mon Jul 17, 2006 5:43 pm ] |
Post subject: | |
I would suggest reading all of the information into a Hashtable, then printing it out. It will make for a more interesting exercise. |
Author: | r.3volved [ Mon Jul 17, 2006 5:48 pm ] | ||||||
Post subject: | |||||||
Well your first instict should be to create a class for students.
It doesn't look like you've been taught arrays yet so I've made it simple for you to follow for now. Using an integer array to hold grades would make this class a little more efficient, but don't worry about it for now (unless you know of arrays...in which case, you should be using them). So to make a new object of Student and add data it's simply:
To get data from a particular student object then we use:
|
Author: | wtd [ Mon Jul 17, 2006 6:44 pm ] | ||
Post subject: | |||
r.3volved wrote: It doesn't look like you've been taught arrays yet so I've made it simple for you to follow for now.
No time like the present to learn. Also, you should probably be using an ArrayList or such. Then you have something that can be of variable length. Oh, and the casting in your getAverage method is incorrectly positioned.
|
Author: | Caceres [ Tue Jul 18, 2006 9:51 am ] | ||
Post subject: | |||
Thanks to everyone who helped me. I made up my own way. The easy way lol. I forgot how to do loops and stuff. So it's kind of long.
|
Author: | r.3volved [ Tue Jul 18, 2006 11:10 am ] |
Post subject: | |
mmmmm... forgot how to do loops?? ![]() You're gonna make every programmer on this board cry with that attitude You obviously have access to the internet, or you wouldn't be posting here...ummm here's a helpful site if you "forget" concepts or have any question about anything -> http://www.google.ca Java has great documentation...chances are you will end up with a horrible mark on that project if you've forgotten loops If you have no drive to figure out what you don't already know, or to recap the things you've forgotten, then maybe you've chosen the wrong field to pursue |
Author: | OneOffDriveByPoster [ Tue Jul 18, 2006 8:38 pm ] |
Post subject: | |
@Caceres: you really should use a loop--who says that you will have the same number of students in each file? Focus on the loop; you do not need arrays here. Simplicity is key. |
Author: | r.3volved [ Tue Jul 18, 2006 8:47 pm ] |
Post subject: | |
for sure you should be using arrays here ...who says there's the same amount of grades for each student?? |
Author: | OneOffDriveByPoster [ Tue Jul 18, 2006 9:04 pm ] |
Post subject: | |
r.3volved wrote: for sure you should be using arrays here
...who says there's the same amount of grades for each student?? Why would I need an array even if there is a variable number of marks per student? |
Author: | r.3volved [ Tue Jul 18, 2006 10:33 pm ] |
Post subject: | |
so you can iterate through the array in a for loop from 0 to the amount of values it contains and then divide by the array elements +1 for average The point is, you don't want ANY hard coded values in your code |
Author: | OneOffDriveByPoster [ Tue Jul 18, 2006 10:46 pm ] |
Post subject: | |
r.3volved wrote: so you can iterate through the array in a for loop from 0 to the amount of values it contains and then divide by the array elements +1 for average
The point is, you don't want ANY hard coded values in your code ...and the initial size of your array (which I still do not see) is not a hard coded value? |
Author: | Aziz [ Wed Jul 19, 2006 9:01 am ] |
Post subject: | |
OneOffDriveByPoster wrote: r.3volved wrote: so you can iterate through the array in a for loop from 0 to the amount of values it contains and then divide by the array elements +1 for average
The point is, you don't want ANY hard coded values in your code ...and the initial size of your array (which I still do not see) is not a hard coded value? ArrayList comes in handy here ![]() |
Author: | r.3volved [ Wed Jul 19, 2006 11:25 am ] |
Post subject: | |
ArrayList = http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html If you're a noob and don't understand arraylists then set your array to the maximum number of classes that a student can take (instansiating them all to 0) and identify if the value != 0 as you parse through the array. Don't criticize just because you don't understand |
Author: | Aziz [ Wed Jul 19, 2006 4:09 pm ] | ||||||
Post subject: | |||||||
Or, define the array as
and when you add a new value:
Although, please, for the love of learning, and ease, learn to use ArrayList. There's even a tutorial here on it (if not ArrayList, then Vector, which is similar) To make an ArrayList:
Please, look at the tutorial. Don't stop at what you know. You take on projects not only to make something, but to learn in the process. |
Author: | wtd [ Wed Jul 19, 2006 6:26 pm ] | ||
Post subject: | |||
When you declare a:
You make baby bunny rabbits cry. Declaring an ArrayList this way throws out all of the benefits generics provide. |
Author: | OneOffDriveByPoster [ Wed Jul 19, 2006 7:02 pm ] |
Post subject: | |
r.3volved wrote: Don't criticize just because you don't understand
I understand that the program does not need an array. Do you--or do you not--understand? |
Author: | [Gandalf] [ Wed Jul 19, 2006 7:18 pm ] |
Post subject: | |
r.3volved, you originally said array, not ArrayList. That said, I believe OneOffDriveByPoster was pointing out your mistake, ie: You say: Use an array, avoid hard coded values. He says: Arrays are hard coded. The general problem, though, is not what I tried to clear up above. It is that you do not need to store the value of each mark in an array. You can instead just add the mark to the total when it is entered and find the average by dividing the total by the amount of marks. Don't complicate things when you don't need to. |
Author: | OneOffDriveByPoster [ Wed Jul 19, 2006 7:22 pm ] |
Post subject: | |
[Gandalf] wrote: You can instead just add the mark to the total when it is entered and find the average by dividing the total by the amount of marks. Don't complicate things when you don't need to.
Thank you. Now I think this thread can rest in peace. :D |
Author: | wtd [ Wed Jul 19, 2006 7:34 pm ] |
Post subject: | |
[Gandalf] wrote: The general problem, though, is not what I tried to clear up above. It is that you do not need to store the value of each mark in an array. You can instead just add the mark to the total when it is entered and find the average by dividing the total by the amount of marks.
Let's put it this way... Do it both ways. They both have educational value. |
Author: | Aziz [ Thu Jul 20, 2006 7:47 am ] | ||
Post subject: | |||
wtd wrote: When you declare a:
You make baby bunny rabbits cry. Declaring an ArrayList this way throws out all of the benefits generics provide. Clarification to avoid confusion: I said <Object> as in <PlaceAnyObjectHere>. I didn't use <E> because most likely that would confuse him. In layman's terms, if you want an array of, say, Strings, it would be ArrayList<String>. Okay, that's it then. I'm done with this topic. |