
-----------------------------------
Caceres
Mon Jul 17, 2006 4:52 pm

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:
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:
import TerminalIO.KeyboardReader;
import becker.io.*;
import java.io.*;

public class ass43 {
   public static void main(String[] args) {
      File f1 = new File("stds.txt");
      String smith,jones,ricci,lombardi,santos;
      double s_1,s_2,s_3,s_4;
      double j_1,j_2,j_3,j_4;
      double r_1,r_2,r_3,r_4;
      double l_1,l_2,l_3,l_4;
      double sa_1,sa_2,sa_3,sa_4;

      if(f1.exists()) {
         TextInput in = new TextInput("stds.txt");

         while(!in.eofIsAvailable()) {
            smith = in.readWord();
            System.out.print(smith+"\t");
            s_1 = in.readDouble();
            System.out.print(s_1+"\t");
            s_2 = in.readDouble();
            System.out.print(s_2+"\t");
            s_3 = in.readDouble();
            System.out.print(s_3+"\t");
            s_4 = in.readDouble();
            System.out.println(s_4+"\t");
            
            
          
            jones = in.readWord();
            System.out.print(jones+"\t");
            j_1 = in.readDouble();
            System.out.print(j_1+"\t");
            j_2 = in.readDouble();
            System.out.print(j_2+"\t");
            j_3 = in.readDouble();
            System.out.print(j_3+"\t");
            j_4 = in.readDouble();
            System.out.println(j_4+"\t");
            
            
            
            ricci = in.readWord();
            System.out.print(ricci+"\t");
            r_1 = in.readDouble();
            System.out.print(r_1+"\t");
            r_2 = in.readDouble();
            System.out.print(r_2+"\t");
            r_3 = in.readDouble();
            System.out.print(r_3+"\t");
            r_4 = in.readDouble();
            System.out.println(r_4+"\t");
            
            
            
            lombardi = in.readWord();
            System.out.print(lombardi+"\t");
            l_1 = in.readDouble();
            System.out.print(l_1+"\t");
            l_2 = in.readDouble();
            System.out.print(l_2+"\t");
            l_3 = in.readDouble();
            System.out.print(l_3+"\t");
            l_4 = in.readDouble();
            System.out.println(l_4+"\t");
            


            santos = in.readWord();
            System.out.print(santos+"\t");
            sa_1 = in.readDouble();
            System.out.print(sa_1+"\t");
            sa_2 = in.readDouble();            
            System.out.print(sa_2+"\t");
            sa_3 = in.readDouble();
            System.out.print(sa_3+"\t");
            sa_4 = in.readDouble();            
            System.out.println(sa_4+"\t");
         }
         in.close();
      } 
      else
         System.out.print("I could not open the file stds.\n");
   }
}

I keep on getting an error..It shows this...
http://www.maj.com/gallery/galeontiger/GFX/error.jpg

-----------------------------------
Caceres
Mon Jul 17, 2006 4:53 pm


-----------------------------------
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

-----------------------------------
wtd
Mon Jul 17, 2006 5:19 pm


-----------------------------------
Are you required to use classes that are not in the standard Java library?

-----------------------------------
Caceres
Mon Jul 17, 2006 5:24 pm


-----------------------------------
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.
 :?

-----------------------------------
rizzix
Mon Jul 17, 2006 5:28 pm


-----------------------------------
Here's an example using java 1.5's Scanner class..

import java.util.Scanner;
import java.util.regex.*;

class test {
    public static void main(String

-----------------------------------
wtd
Mon Jul 17, 2006 5:43 pm


-----------------------------------
I would suggest reading all of the information into a Hashtable, then printing it out.  It will make for a more interesting exercise.

-----------------------------------
r.3volved
Mon Jul 17, 2006 5:48 pm


-----------------------------------
Well your first instict should be to create a class for students.


public class Student
{
   private String name_;
   private int mark1_;
   private int mark2_;
   private int mark3_;
   private int mark4_;

   public Student() {}

   //MUTATORS
   public void setMark1(int myMark) {mark1_ = myMark;}
   public void setMark2(int myMark) {mark2_ = myMark;}
   public void setMark3(int myMark) {mark3_ = myMark;}
   public void setMark4(int myMark) {mark4_ = myMark;}

   //ACCESSORS
   public int getMark1() {return mark1_;}
   public int getMark2() {retrun mark2_;}
   public int getMark3() {return mark3_;}
   public int getMark4() {return mark4_;}

   public float getAverage() 
   {
      return (float)((mark1_ + mark2_ + mark3_ + mark4_)/4);
   }
}


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:


Student myStudent = new Student();
myStudent.setMark1(some_number);


To get data from a particular student object then we use:


myStudent.getMark1(); //returns mark1 from myStudent object
myStudent.getAverage();


-----------------------------------
wtd
Mon Jul 17, 2006 6:44 pm


-----------------------------------
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.

class Student
{
   private String name;
   private ArrayList grades;

   public Student(String name) {
      this.name = name;
      this.grades = new ArrayList();
   }

   public void addGrade(int newGrade) {
      grades.add(newGrade);
   }

   public ArrayList getGrades() {
      return grades;
   }

   public int numberOfGrades() {
      return grades.size();
   }

   public double averageGrade() {
      int sum = 0;
      
      Iterator iter = grades.iterator();
      while (iter.hasNext()) sum += iter.next();
   
      return (double)sum / numberOfGrades();
   }
}

-----------------------------------
Caceres
Tue Jul 18, 2006 9:51 am


-----------------------------------
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.  

import TerminalIO.KeyboardReader; 
import becker.io.*; 
import java.io.*; 

public class ass43 { //file name 
public static void main(String[] args) { 
File f1 = new File("stds.txt"); //variable for the file being used 
String smith,jones,ricci,lombardi,santos; //variables for the names of the students 
double s_1,s_2,s_3,s_4; //variables for the marks of Smith 
double j_1,j_2,j_3,j_4; //variables for the marks of Jones 
double r_1,r_2,r_3,r_4; //variables for the marks of Ricco 
double l_1,l_2,l_3,l_4; //variables for the marks of Lombardi 
double sa_1,sa_2,sa_3,sa_4; //variables for the marks of Santos 
double average1,average2,average3,average4,average5; //variables for the averages of each student 

if(f1.exists()) { //statements that will occur if the file exists 
TextInput in = new TextInput("stds.txt"); //variable for the text that will be inputted 

while(!in.eofIsAvailable()) { 


smith = in.readWord(); 
System.out.print(smith+"\t\t"); 
s_1 = in.readDouble(); 
System.out.print(s_1+"\t"); 
s_2 = in.readDouble(); 
System.out.print(s_2+"\t"); 
s_3 = in.readDouble(); 
System.out.print(s_3+"\t"); 
s_4 = in.readDouble(); 
System.out.println(s_4+"\t"); 
/*This bunching of statements obtains the data 
from the file for smith. It reads the first line 
and places them with their appropriate variables.*/ 


in.readLine(); 
jones = in.readWord(); 
System.out.print(jones+"\t\t"); 
j_1 = in.readDouble(); 
System.out.print(j_1+"\t"); 
j_2 = in.readDouble(); 
System.out.print(j_2+"\t"); 
j_3 = in.readDouble(); 
System.out.print(j_3+"\t"); 
j_4 = in.readDouble(); 
System.out.println(j_4+"\t"); 
/*This bunching of statements obtains the data 
from the file for Jones. It reads the second line 
and places them with their appropriate variables.*/ 


in.readLine(); 
ricci = in.readWord(); 
System.out.print(ricci+"\t\t"); 
r_1 = in.readDouble(); 
System.out.print(r_1+"\t"); 
r_2 = in.readDouble(); 
System.out.print(r_2+"\t"); 
r_3 = in.readDouble(); 
System.out.print(r_3+"\t"); 
r_4 = in.readDouble(); 
System.out.println(r_4+"\t"); 
/*This bunching of statements obtains the data 
from the file for Ricci. It reads the third line 
and places them with their appropriate variables.*/ 


in.readLine(); 
lombardi = in.readWord(); 
System.out.print(lombardi+"\t"); 
l_1 = in.readDouble(); 
System.out.print(l_1+"\t"); 
l_2 = in.readDouble(); 
System.out.print(l_2+"\t"); 
l_3 = in.readDouble(); 
System.out.print(l_3+"\t"); 
l_4 = in.readDouble(); 
System.out.println(l_4+"\t"); 
/*This bunching of statements obtains the data 
from the file for Lombardi. It reads the fourth line 
and places them with their appropriate variables.*/ 


in.readLine(); 
santos = in.readWord(); 
System.out.print(santos+"\t\t"); 
sa_1 = in.readDouble(); 
System.out.print(sa_1+"\t"); 
sa_2 = in.readDouble(); 
System.out.print(sa_2+"\t"); 
sa_3 = in.readDouble(); 
System.out.print(sa_3+"\t"); 
sa_4 = in.readDouble(); 
System.out.println(sa_4+"\t"); 
/*This bunching of statements obtains the data 
from the file for Santos. It reads the fifth line 
and places them with their appropriate variables.*/ 


average1=(s_1+s_2+s_3+s_4)/4; 
System.out.println("\nThe average of Smith's marks is "+average1+"."); 
average2=(j_1+j_2+j_3+j_4)/4; 
System.out.println("\nThe average of Jones' marks is "+average2+"."); 
average3=(r_1+r_2+r_3+r_4)/4; 
System.out.println("\nThe average of Ricci's marks is "+average3+"."); 
average4=(l_1+l_2+l_3+l_4)/4; 
System.out.println("\nThe average of Lombardi's marks is "+average4+"."); 
average5=(sa_1+sa_2+sa_3+sa_4)/4; 
System.out.println("\nThe average of Santos' marks is "+average5+".\n\n"); 
/*The above statements produce the average for each of the students. it takes 
all four of their marks, adds them all up and then divides by the number of 
marks(which is four). It also outputs the averages, so that the user will see 
the info.*/ 


return; 
} 
in.close(); //closes the file that has been opened 
} 
else 
System.out.print("I could not open the file stds.\n"); 
/*If the file cannot be opened, then this is the statement that is return. 
And the program won't run.*/ 
} 
}

-----------------------------------
r.3volved
Tue Jul 18, 2006 11:10 am


-----------------------------------
mmmmm...
forgot how to do loops?? :shock: 

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

-----------------------------------
OneOffDriveByPoster
Tue Jul 18, 2006 8:38 pm


-----------------------------------
@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.

-----------------------------------
r.3volved
Tue Jul 18, 2006 8:47 pm


-----------------------------------
for sure you should be using arrays here

...who says there's the same amount of grades for each student??

-----------------------------------
OneOffDriveByPoster
Tue Jul 18, 2006 9:04 pm


-----------------------------------
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?

-----------------------------------
r.3volved
Tue Jul 18, 2006 10:33 pm


-----------------------------------
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

-----------------------------------
OneOffDriveByPoster
Tue Jul 18, 2006 10:46 pm


-----------------------------------
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?

-----------------------------------
Aziz
Wed Jul 19, 2006 9:01 am


-----------------------------------
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 :)

-----------------------------------
r.3volved
Wed Jul 19, 2006 11:25 am


-----------------------------------
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

-----------------------------------
Aziz
Wed Jul 19, 2006 4:09 pm


-----------------------------------
Or, define the array as

int[] array;

and when you add a new value:

newArray = new int

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:

ArrayList array;

array = new ArrayList(0);

value = readLine();

array.add(value);

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.

-----------------------------------
wtd
Wed Jul 19, 2006 6:26 pm


-----------------------------------
When you declare a:

ArrayList

You make baby bunny rabbits cry.  Declaring an ArrayList this way throws out all of the benefits generics provide.

-----------------------------------
OneOffDriveByPoster
Wed Jul 19, 2006 7:02 pm


-----------------------------------
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?

-----------------------------------
[Gandalf]
Wed Jul 19, 2006 7:18 pm


-----------------------------------
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.

-----------------------------------
OneOffDriveByPoster
Wed Jul 19, 2006 7:22 pm


-----------------------------------
"]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

-----------------------------------
wtd
Wed Jul 19, 2006 7:34 pm


-----------------------------------
"]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.

-----------------------------------
Aziz
Thu Jul 20, 2006 7:47 am


-----------------------------------
When you declare a:

ArrayList

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  as in . I didn't use  because most likely that would confuse him. In layman's terms, if you want an array of, say, Strings, it would be ArrayList. Okay, that's it then. I'm done with this topic.
