Arrays and File Reading program

HELP ME PLEASE!!

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2443 Views - Last Post: 30 September 2007 - 12:15 PM Rate Topic: -----

#1 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Arrays and File Reading program

Post icon  Posted 26 September 2007 - 06:08 PM

Hello everyone,

I find myself in desperate straits with this program I am trying to write. I'm going to first describe what I'm trying to accomplish, then post my two classes, then the file used in conjunction. I know it sounds like a lot, but I think I have almost all the code done, I just can't put it all together.

Okay, My program should read a file containing a name and three test grades for several people, then calculate the average score and letter grade for each person, print all this to another file at one time(everything is supposed to be stored in an array), open this file for reading, and print the file to the screen as it reads.

I guess I might need a method to write al the array elements to the text file one line at a time, and some sort of loop in main, but I am just so so befuddled and hoping for some expertise. Thank you so much in advance.

//BELOW IS SERVICE CLASS, FOLLOWED BY DRIVER CLASS, FOLLOWED BY GIVEN TEXT FILE:

import java.io.*;
import java.util.*;

public class Grades
{
	String name;
	int test1;
	int test2;
	int test3;
	double average;
	String grade;
	
	public Grades()
	{
		name="";
		test1=0;
		test2=0;
		test3=0;
		average=0.0;
		grade="";
	}
	
	public Grades(String n,int t1,int t2,int t3,double avg,String letter)
	{
		name=n;
		test1=t1;
		test2=t2;
		test3=t3;
		average=avg;
		grade=letter;
	}
	
	public void setName(String newName)
	{
		name=newName;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setTest1(int newTest1)
	{
		test1=newTest1;
	}
	
	public int getTest1()
	{
		return test1;
	}
	
	public void setTest2(int newTest2)
	{
		test2=newTest2;
	}
	
	public int getTest2()
	{
		return test2;
	}
	
	public void setTest3(int newTest3)
	{
		test3=newTest3;
	}
	
	public int getTest3()
	{
		return test3;
	}
	
	public void setAverage(double newAverage)
	{
		average=newAverage;
	}
	
	public double getAverage()
	{
		return average;
	}
	
	public void setGrade(String newGrade)
	{
		grade=newGrade;
	}
	
	public String getGrade()
	{
		return grade;
	}
	
	public double averageGrades()
	{
		double average=(test1+test2+test3)/3;
		return average;
	}
	
	public String grades()
	{
		if(average >=90)	
			grade="A";
		else if(average >=80)
			grade="B";
		else if(average >=70)
			grade="C";
		else
			grade="F";
			
		return grade;
	}
	

	public boolean readFromFile(BufferedReader inputFile)throws IOException
	{
		String str;
	
		name=inputFile.readLine();
		if (name!=null){
			str=inputFile.readLine();
			test1=Integer.parseInt(str);
		
			str=inputFile.readLine();
			test2=Integer.parseInt(str);
		
			str=inputFile.readLine();
			test3=Integer.parseInt(str);
			
			return true;
		}
		return false;	
	}
	
	public void print()
	{
		System.out.println(name);	
		System.out.println(test1);
		System.out.println(test2);
		System.out.println(test3);
	}		
}


*****************************************

import java.io.*;
import java.util.*;

public class GradesDriver
{

	public static String getFileName()
	{
		Scanner scan=new Scanner(System.in);
		String myFile;
		
		System.out.print("Enter the file name please: ");
		myFile=scan.nextLine();
		return myFile;
	}


	public static void main(String[] args)throws IOException
	{
		Grades [] g = new Grades[10];
		Grades temporary = new Grades();
		
		String myFile=getFileName();
		
		FileReader freader=new FileReader(myFile);
		BufferedReader inputFile=new BufferedReader(freader);
		
		FileWriter fwriter=new FileWriter(myFile);
		PrintWriter outputFile=new PrintWriter(fwriter);
	}
}





***********************************************

Ted Sexton
67
78
50
Dori Andrew
78
83
80
Maggie Smith
90
94
89
Robert Reed
70
76
80

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays and File Reading program

#2 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Arrays and File Reading program

Posted 26 September 2007 - 06:15 PM

i wrote a program kinda like this a couple weeks ago.. ill provide the source for you so you can look at it and see it helps you... gotta go study now though :( basically waht it does is read in sensors readings and then do some calculations and output to file.

Description:
A serious of 3 seismic sensors are planted along a fault line. These sensors constantly detect tremors that occur and record them to a main computer to which all three are connected. At midnight every night, the computer saves all of the observations from the previous 24 hours in a data file. The file consists of a pair of numbers for each reading; the first number signifies the sensor number (1, 2, or 3) and the second number is the electronic reading. Any time a sensor is calibrated or an incorrect reading is recorded, the sensor number is switched to negative. All negative readings should be disregarded in calculations.

The readings recorded are specific to the micro voltage values returned by the actual sensors. All of these readings must be converted to physical units by the formula

value =(reading-62.5)/12.31

The analysts require a summary report to be generated from this file every day. This report should be saved to an output file and contain the following information:

Number of invalid readings recorded in file:

Number of readings for sensor 1:
Sum of values recorded by sensor 1:
Average values recorded by sensor 1:

Number of readings for sensor 2:
Sum of values recorded by sensor 2:
Average values recorded by sensor 2:

Number of readings for sensor 3:
Sum of values recorded by sensor 3:
Average values recorded by sensor 3:

Total number of (valid) readings for all sensors:
Total sum of values recorded by all sensors:
Total average of values recorded by all sensors:

The output must be formatted to contain three digits after the decimal point for each floating-point number.

   import java.io.FileReader;
   import java.io.BufferedReader;
   import java.io.FileWriter;
   import java.io.PrintWriter;
   import java.io.IOException;

	public class Sensors {
	  private Sensor sensor1 = new Sensor(); // sensor object 1
	  private Sensor sensor2 = new Sensor(); // sensor object 2
	  private Sensor sensor3 = new Sensor(); // sensor object 3
	  private int bogus; // holds the number of non-normal readings
   
	   public Sensors() {
	  
		 bogus = 0;
	  }// end constructor
	  
   	
	   //Proccess the data contained in input.dat
	   public void processData() throws IOException {
		 // get the input file ready for reading
		 BufferedReader input = new BufferedReader(new FileReader("input.dat"));
	  
		 String inputLine; // Read line from file
	  
		 inputLine = input.readLine();// read in the first line
	  
		  //loop through all data points
		 while (inputLine != null) {
			int sensor; // Sensor Number
		 	//see if this is a bogus reading
			if (inputLine.substring(0,1).equals("-"))
			   bogus++;
			else {
			   sensor = Integer.parseInt(inputLine.substring(0, 1));
			//update the sensor based on which sensor number is contained int sensor
			   if (sensor == 1)
				  sensor1.update(Double.parseDouble(inputLine.substring(2)));
			   else if (sensor == 2)
				  sensor2.update(Double.parseDouble(inputLine.substring(2)));
			   else if (sensor == 3)
				  sensor3.update(Double.parseDouble(inputLine.substring(2)));
			}//end else
			   
			inputLine=input.readLine();//move to next line
		 }//end while
	  
		 input.close();// finished reading, close.
	  
	  }// end processData
   
	   public void toFile() throws IOException {
		 PrintWriter out = new PrintWriter(new FileWriter("results.dat"));
	  
		 // print the number of non-normal readings
		 out.println("Number of Invalid readings recorded in file:\t"+bogus+"\n");
	  
		 // print the results for sensor1 to the file
		 out.println("Number of readings for sensor1:\t"+sensor1.getCount());
		 out.printf("Sum of values recorded by sensor1:\t%.3f\n", sensor1.getTotal());
		 out.printf("Average values recorded by sensor1:\t%.3f\n\n", sensor1.getAverage());
	  
		 // print results for sensor2 to the file
		 out.println("Number of readings for sensor2:\t"+sensor2.getCount());
		 out.printf("Sum of values recorded by sensor2:\t%.3f\n", sensor2.getTotal());
		 out.printf("Average values recorded by sensor2\t%.3f\n\n", sensor2.getAverage());
	  
		 // print results for sensor3 to the file
		 out.println("Number of readings for sensor3:\t\t"+sensor3.getCount());
		 out.printf("Sum of values recorded by sensor3:\t\t%.3f\n", sensor3.getTotal());
		 out.printf("Average values recorded by sensor3:\t%.3f\n\n", sensor3.getAverage());
			  
			//process the totals	
		 int count=sensor1.getCount()+sensor2.getCount()+sensor3.getCount();
		 double total= sensor1.getTotal()+sensor2.getTotal()+sensor3.getTotal();
		 double average=(sensor1.getAverage()+sensor2.getAverage()+sensor3.getAverage())/3;
		 // print the totals to the file
		 out.println("Total number of readings for all sensors:\t"+count);
		 out.printf("Total sum of values recorded by all sensors:\t%.3f\n", total);
		 out.printf("Total average of values recorded by all sensors:\t%.3f\n\n", average);
			
		 out.close();//close the output file
	  }// end toFile
   
	   public static void main(String args[]) throws IOException {
		 Sensors test = new Sensors();
		 test.processData();
		 test.toFile();
	  }// end main
   
   }// end SensorRunner




	public class Sensor {
	  int count; // hold the total number of readings
   
	  double totalReading; // holds the total of the readings
   
	   public Sensor() {
		 count = 0;
		 totalReading = 0;
	  }// end constructor
   
	   public void update(double reading) {
		 count++;
		 reading = reading - 62.5;
		 reading = reading/12.31;
		 totalReading =totalReading+reading;
	  }// end update
   
	   public int getCount() {
		 return count;
	  }// end getCount
   
	   public double getTotal() {
		 return totalReading;
	  }// end getTotal
   
	   public double getAverage() {
		 return (totalReading / count);
	  }// end getAverage
   }// end Sensors



Input file:
2 1363.636
3 1383.117
3 1102.597
2 1422.078
2 1441.558
1 461.039
2 1480.519
1 501.020
1 1519.480
2 1538.961
3 158.442
3 1577.922
-2 1597.402
3 116.883
1 1636.364
2 1655.844
2 1175.325
1 1694.805
2 1714.286
3 733.766
1 1253.247
2 1772.727
3 392.208
1 1811.688
2 131.169
-1 1850.649
-1 1870.130
-2 1889.610
-3 709.091
2 928.571
2 148.052

Output file:
Number of Invalid readings recorded in file: 5

Number of readings for sensor1: 7
Sum of values recorded by sensor1: 685.633
Average values recorded by sensor1: 97.948

Number of readings for sensor2: 12
Sum of values recorded by sensor2: 1139.133
Average values recorded by sensor2 94.928

Number of readings for sensor3: 7
Sum of values recorded by sensor3: 408.403
Average values recorded by sensor3: 58.343

Total number of readings for all sensors: 26
Total sum of values recorded by all sensors: 2233.168
Total average of values recorded by all sensors: 83.740
Was This Post Helpful? 0
  • +
  • -

#3 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 09:10 AM

Hello again,

Slacker posted an awesome example of a program he wrote to help me out, and it does indeed do similar things. But the combination of me being totally totally lost and my need to use a matrix and stuff is resulting in me still being totally stuck. I have to complete this program by Saturday and I'm so nervous I won't! I have spent hours and hours writing what I have so far, and everytime I add something else I just find out it's incorrect.

Thank you so much for any more assistance everyone!
Was This Post Helpful? 0
  • +
  • -

#4 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 09:30 AM

okay well first off i dont see why you have all these

	public void setName(String newName)
	{
		name=newName;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setTest1(int newTest1)
	{
		test1=newTest1;
	}
	
	public int getTest1()
	{
		return test1;
	}
	
	public void setTest2(int newTest2)
	{
		test2=newTest2;
	}
	
	public int getTest2()
	{
		return test2;
	}
	
	public void setTest3(int newTest3)
	{
		test3=newTest3;
	}
	
	public int getTest3()
	{
		return test3;
	}
	
	public void setAverage(double newAverage)
	{
		average=newAverage;
	}
	
	public double getAverage()
	{
		return average;
	}
	
	public void setGrade(String newGrade)
	{
		grade=newGrade;
	}
	
	public String getGrade()
	{
		return grade;
	}


unless your assignment says to access this stuff from another class i wouldn't use another class.. just stick your main method in your original grade class.

Secondly, atleast your code compiles... mostly what we get here doesn't lol. gimmie a couple minutes to look through it.
Was This Post Helpful? 0
  • +
  • -

#5 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 10:33 AM

Quote

unless your assignment says to access this stuff from another class i wouldn't use another class.. just stick your main method in your original grade class.

Secondly, atleast your code compiles... mostly what we get here doesn't lol. gimmie a couple minutes to look through it.


Yeah the assignment says we have to have all those accessors and mutators in the service class, and then main in a seperate driver class.
Was This Post Helpful? 0
  • +
  • -

#6 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 10:42 AM

can you write exactly waht the assignment says here?
Was This Post Helpful? 0
  • +
  • -

#7 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 11:06 AM

View Postsl4ck3r, on 27 Sep, 2007 - 10:42 AM, said:

can you write exactly waht the assignment says here?


Yes I will put it below here. When I showed my teacher the code I have so far, she said it was good and that by adding some stuff to main and just a couple things to the service class it would be perfect. The problem is I won't see my teacher again until the program is due! I really appreciate the time your taking to assist me. Here it is:

THIS LAB DEALS WITH AN ARRAY OF CLASS OBJECTS, READING AND WRITING TO A FILE USING MIXED DATA TYPE INPUT, AND MANIPULATING ARRAYS. YOU WILL READ DATA FROM AN EXISTING FILE AND THEN WRITE DATA TO A DIFFERENT FILE.

1.WRITE A CLASS, GRADES, THAT CONTAINS A KID'S NAME AND GRADE ON 3 TESTS(INTEGERS), AVERAGE(DOUBLE), AND A LETTER GRADE. THE CLASS SHOULD HAVE ALL OF THE NORMAL THINGS EXPECTED IN A STAND ALONE CLASS:
A. DEFAULT AND OVERLOAD CONSTRUCTORS
B. 6 ACCESSORS AND 6 MUTATORS
C. AVERAGE() METHOD TO DETERMINE AVERAGE OF 3 TESTS AND RETURN IT ONCE TEST SCORES HAVE BEEN
READ.
D. GRADES() METHOD DETERMINES LETTER GRADE BASED UPON AVERAGE:
A=90-100,B=80-89,C=70-79,F=BELOW 70
E. READFROMFILE() METHOD TO READ NAME AND 3 TEST SCORES ONLY, THEN THE AVERAGE() AND GRADES() FILL IN THE OTHER PART OF THE RECORD. THIS METHOD WILL READ UNTIL END OF FILE.
F. PRINT() PRINTS ONE RECORD TO THE SCREEN.
G. WRITETOFILE() STATIC METHOD THAT WRITES ALL ARRAY ELEMENTS TO A TEXT FILE, ONE PIECE OF DATA PER LINE.
H. GETFILENAME() STATIC METHOD THAT GETS NAME OF FILE FROM USER. WILL BE CALLED TWICE(ONCE TO OPEN FILE TO READ INTO THE ARRAY, AND ONCE TO WRITE ARRAY TO ANOTHER FILE)
I. THE ARRAY IN MAIN SHOULD HOLD 10 GRADE OBJECTS. YOU WILL PROBABLY NEED TO TRACK THE NUMBER OF ELEMENTS YOU ARE READING BUT DON'T PUT THIS VARIABLE IN THE GRADE CLASS.

THIS PROGRAM WILL READ THE PROVIDED FILE, CALCULATE AVERAGE AND GRADE, PRINT ALL THIS TO A FILE AT ONCE( YOU STORE EVERYTHING IN THE ARRAY), OPEN THIS FILE FOR READING AND PRINT THE FILE TO THE SCREEN AS IT READS.
Was This Post Helpful? 0
  • +
  • -

#8 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 01:59 PM

Does this make sense?
Was This Post Helpful? 0
  • +
  • -

#9 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 04:44 PM

first off you probably want to add another file name input so you wont be over writing your grades.txt eveytime you run the program with the results.
Was This Post Helpful? 0
  • +
  • -

#10 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 04:54 PM

View Postsl4ck3r, on 27 Sep, 2007 - 04:44 PM, said:

first off you probably want to add another file name input so you wont be over writing your grades.txt eveytime you run the program with the results.


Well if you mean the getFileName method, I was told to just call it twice, once for opening the grades file that is provided, and once for naming the file that I'm supposed to write the final stuff to.
How would I go about getting the grades file read into my array, and writing that stuff in the array into a different file?
Was This Post Helpful? 0
  • +
  • -

#11 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 05:05 PM


		 Grades [] g = new Grades[10];
		 int counter=0;
		 Grades temp;
		
		 String myFile=getInputFileName();
		
		 FileReader freader=new FileReader(myFile);
		 BufferedReader inputFile=new BufferedReader(freader);
		
		 while(inputFile.ready()) {
		 
		 temp=new Grades();
		 temp.readFromFile(inputFile);
		 g[counter]=temp;
		 counter++;
		 }



then you need to make a method pretty much like the readfromfile method but to print to the file. you can pass the file writer just like in the the read from file they just type NameOfOutputFile.println("what you want to print") then at the end of your main method you MUST close your outputfile with outputFile.close() or the resulting document will be blank.

This post has been edited by sl4ck3r: 27 September 2007 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#12 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 07:13 PM

View Postsl4ck3r, on 27 Sep, 2007 - 05:05 PM, said:


		 Grades [] g = new Grades[10];
		 int counter=0;
		 Grades temp;
		
		 String myFile=getInputFileName();
		
		 FileReader freader=new FileReader(myFile);
		 BufferedReader inputFile=new BufferedReader(freader);
		
		 while(inputFile.ready()) {
		 
		 temp=new Grades();
		 temp.readFromFile(inputFile);
		 g[counter]=temp;
		 counter++;
		 }



then you need to make a method pretty much like the readfromfile method but to print to the file. you can pass the file writer just like in the the read from file they just type NameOfOutputFile.println("what you want to print") then at the end of your main method you MUST close your outputfile with outputFile.close() or the resulting document will be blank.


Ok well taking what you suggested, I made some changes and added some things, and I know it's still not complete. Not to mention now I'm getting some errors, like that my service class can't find symbol on variable g, which is my matrix that i declared in main. Here is my updated code:
import java.io.*;
import java.util.*;

public class Grades
{
	String name;
	int test1;
	int test2;
	int test3;
	double average;
	String grade;
	
	public Grades()
	{
		name="";
		test1=0;
		test2=0;
		test3=0;
		average=0.0;
		grade="";
	}
	
	public Grades(String n,int t1,int t2,int t3,double avg,String letter)
	{
		name=n;
		test1=t1;
		test2=t2;
		test3=t3;
		average=avg;
		grade=letter;
	}
	
	public void setName(String newName)
	{
		name=newName;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setTest1(int newTest1)
	{
		test1=newTest1;
	}
	
	public int getTest1()
	{
		return test1;
	}
	
	public void setTest2(int newTest2)
	{
		test2=newTest2;
	}
	
	public int getTest2()
	{
		return test2;
	}
	
	public void setTest3(int newTest3)
	{
		test3=newTest3;
	}
	
	public int getTest3()
	{
		return test3;
	}
	
	public void setAverage(double newAverage)
	{
		average=newAverage;
	}
	
	public double getAverage()
	{
		return average;
	}
	
	public void setGrade(String newGrade)
	{
		grade=newGrade;
	}
	
	public String getGrade()
	{
		return grade;
	}
	
	public double averageGrades()
	{
		double average=(test1+test2+test3)/3;
		return average;
	}
	
	public String grades()
	{
		if(average >=90)	
			grade="A";
		else if(average >=80)
			grade="B";
		else if(average >=70)
			grade="C";
		else
			grade="F";
			
		return grade;
	}
	

	public boolean readFromFile(BufferedReader inputFile)throws IOException
	{
		String str;
	
		name=inputFile.readLine();
		if (name!=null){
			str=inputFile.readLine();
			test1=Integer.parseInt(str);
		
			str=inputFile.readLine();
			test2=Integer.parseInt(str);
		
			str=inputFile.readLine();
			test3=Integer.parseInt(str);
			
			return true;
		}
		return false;	
	}
	
	public static void writeToFile(PrintWriter outputFile)throws IOException
	{
		for (int index=0; index< g.length; index++)
			outputFile.println(g[index]);
	}
		
	
	public void print()
	{
		System.out.println(name);	
		System.out.println(test1);
		System.out.println(test2);
		System.out.println(test3);
	}		
}

***********************************************8
import java.io.*;
import java.util.*;

public class GradesDriver
{

	public static String getFileName()
	{
		Scanner scan=new Scanner(System.in);
		String myFile;
		
		System.out.print("Enter the file name please: ");
		myFile=scan.nextLine();
		return myFile;
	}


	public static void main(String[] args)throws IOException
	{
		Grades [] g = new Grades[10];
		int counter=0;
		Grades temp;
		
		String myFile=getFileName();
		
		FileReader freader=new FileReader(myFile);
		BufferedReader inputFile=new BufferedReader(freader);
		
		while(inputFile.ready()) {
			temp=new Grades();
			temp.readFromFile(inputFile);
			g[counter]=temp;
			counter++;
			}
			
		inputFile.close();
		
		FileWriter fwriter=new FileWriter(myFile);
		PrintWriter outputFile=new PrintWriter(fwriter);
	}
}


This post has been edited by jreibe3000: 27 September 2007 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#13 sl4ck3r  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Arrays and File Reading program

Posted 27 September 2007 - 10:12 PM

since the array is in the drivers class not in the grades class it does not have access to the array, nor does it know it exists.... you must pass the array as well. Also when you print to the output file printing g[index] is not going to work because your trying to print an object.. you need to print the data in taht object. which you can do by either doing g[index].datamembername (ie: g[index].name) or by calling the methods you made on that object (ie: g[index].getAverage() ). This is why i originally said those methods were useless because you can access the data members through their class....
Was This Post Helpful? 0
  • +
  • -

#14 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Re: Arrays and File Reading program

Posted 29 September 2007 - 07:38 PM

View Postsl4ck3r, on 27 Sep, 2007 - 10:12 PM, said:

since the array is in the drivers class not in the grades class it does not have access to the array, nor does it know it exists.... you must pass the array as well. Also when you print to the output file printing g[index] is not going to work because your trying to print an object.. you need to print the data in taht object. which you can do by either doing g[index].datamembername (ie: g[index].name) or by calling the methods you made on that object (ie: g[index].getAverage() ). This is why i originally said those methods were useless because you can access the data members through their class....


Okay I messed with the code some more and tried looking up how to do some things I didn't know. The result is I have more code written, but I'm still totally lost, I think I'm doing several things wrong, and I'm horrified I'm not gonna get this thing done by Monday morning, which is the latest she'll accept it. I'm just so confused about things, and I don't see how to accomplish this correctly. I have methods written to calculate the average of the 3 tests and the letter grade for each person, but I don't know how to implement things correctly using my array, loops, and the txt files. I'll post the code I currently have, with comments added to show my intended logic, and hopefully you can help me clear things up. Gratefully indebted.
import java.io.*;
import java.util.*;

public class GradesDriver
{

	public static String getFileName()  //call once to open file to read, and again to write array to diff. file
	{
		Scanner scan=new Scanner(System.in);
		String myFile;
		
		System.out.print("Enter the file name please: ");
		myFile=scan.nextLine();
		return myFile;
	}
	
	public static void writeToFile(g,10)throws IOException // should print all of array g to a new txt file
	{
		FileWriter fwriter=new FileWriter(myFile);
		PrintWriter outputFile=new PrintWriter(fwriter);

		for (int index=0; index< g.length; index++){
			outputFile.println(g[index].print());
			outputFile.println(g[index].averageGrades());
			ouptutFile.println(g[index].grades());
		}
		outputFile.close();
	}
		
	public static void main(String[] args)throws IOException //should be used for method calls
	{
		Grades [] g = new Grades[10];
		int counter=0;
		Grades temp;
		
		String myFile=getFileName();
		
		FileReader freader=new FileReader(myFile);
		BufferedReader inputFile=new BufferedReader(freader);
		
		while(inputFile.ready()) {
			temp=new Grades();
			temp.readFromFile(inputFile);
			g[counter]=temp;
			counter++;
			}
			
		inputFile.close();
		
		String myFile=getFileName();
		String myFile=writeToFile();
		
		outputFile.close();
			
		
		}
}

*********************************************************

import java.io.*;
import java.util.*;

public class Grades
{
	String name;
	int test1;
	int test2;
	int test3;
	double average;
	String grade;
	
	public Grades()
	{
		name="";
		test1=0;
		test2=0;
		test3=0;
		average=0.0;
		grade="";
	}
	
	public Grades(String n,int t1,int t2,int t3,double avg,String letter)
	{
		name=n;
		test1=t1;
		test2=t2;
		test3=t3;
		average=avg;
		grade=letter;
	}
	
	public void setName(String newName)
	{
		name=newName;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setTest1(int newTest1)
	{
		test1=newTest1;
	}
	
	public int getTest1()
	{
		return test1;
	}
	
	public void setTest2(int newTest2)
	{
		test2=newTest2;
	}
	
	public int getTest2()
	{
		return test2;
	}
	
	public void setTest3(int newTest3)
	{
		test3=newTest3;
	}
	
	public int getTest3()
	{
		return test3;
	}
	
	public void setAverage(double newAverage)
	{
		average=newAverage;
	}
	
	public double getAverage()
	{
		return average;
	}
	
	public void setGrade(String newGrade)
	{
		grade=newGrade;
	}
	
	public String getGrade()
	{
		return grade;
	}
	
	public double averageGrades() // this mehod just calculates the test grade for each person
	{
		double average=(test1+test2+test3)/3;
		return average;
	}
	
	public String grades() // this method finds the letter grade for each person
	{
		if(average >=90)	
			grade="A";
		else if(average >=80)
			grade="B";
		else if(average >=70)
			grade="C";
		else
			grade="F";
			
		return grade;
	}
	

	public boolean readFromFile(BufferedReader inputFile)throws IOException { //reads all data from file
		String str;
	
		name=inputFile.readLine();
		if (name!=null){
			str=inputFile.readLine();
			test1=Integer.parseInt(str);
		
			str=inputFile.readLine();
			test2=Integer.parseInt(str);
		
			str=inputFile.readLine();
			test3=Integer.parseInt(str);
			
			return true;
		}
		return false;	
	}
		
	public void print() // prints  all items from file to the screen
	{
		System.out.println(name);	
		System.out.println(test1);
		System.out.println(test2);
		System.out.println(test3);
	}		
}



Was This Post Helpful? 0
  • +
  • -

#15 jreibe3000  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 08-June 07

Re: Arrays and File Reading program

Post icon  Posted 30 September 2007 - 08:57 AM

Hello everyone,

I'd been posting on another thread, but now when I go to view that thread my browser either locks up or does something else wierd, and I thought maybe that's why I wasn't getting any more responses. That being said, I'll just post where I'm at on this new thread. I'm green as can be in programming, and therefore ready to yank every string of hair from my head. This program is due tomorrow morning, and I have spent hours and hours trying to complete it. My teacher had told me that it is almost done, and that I just need to add some things and fix a few mistakes. Please please assist a desperate man!
Thanks in advance to any who take time.

import java.io.*;
import java.util.*;

public class GradesDriver
{

	public static String getFileName()  //call once to open file to read, and again to write array to diff. file
	{
		Scanner scan=new Scanner(System.in);
		String myFile;
		
		System.out.print("Enter the file name please: ");
		myFile=scan.nextLine();
		return myFile;
	}
	
	public static void writeToFile(g,10)throws IOException // should print all of array g to a new txt file
	{
		FileWriter fwriter=new FileWriter(myFile);
		PrintWriter outputFile=new PrintWriter(fwriter);

		for (int index=0; index< g.length; index++){
			outputFile.println(g[index].print());
			outputFile.println(g[index].averageGrades());
			ouptutFile.println(g[index].grades());
		}
		outputFile.close();
	}
		
	public static void main(String[] args)throws IOException //should be used for method calls
	{
		Grades [] g = new Grades[10];
		int counter=0;
		Grades temp;
		
		String myFile=getFileName();
		
		FileReader freader=new FileReader(myFile);
		BufferedReader inputFile=new BufferedReader(freader);
		
		while(inputFile.ready()) {
			temp=new Grades();
			temp.readFromFile(inputFile);
			g[counter]=temp;
			counter++;
			}
			
		inputFile.close();
		
		String myFile=getFileName();
		String myFile=writeToFile();
		
		outputFile.close();
			
		
		}
}

*********************************************************

import java.io.*;
import java.util.*;

public class Grades
{
	String name;
	int test1;
	int test2;
	int test3;
	double average;
	String grade;
	
	public Grades()
	{
		name="";
		test1=0;
		test2=0;
		test3=0;
		average=0.0;
		grade="";
	}
	
	public Grades(String n,int t1,int t2,int t3,double avg,String letter)
	{
		name=n;
		test1=t1;
		test2=t2;
		test3=t3;
		average=avg;
		grade=letter;
	}
	
	public void setName(String newName)
	{
		name=newName;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setTest1(int newTest1)
	{
		test1=newTest1;
	}
	
	public int getTest1()
	{
		return test1;
	}
	
	public void setTest2(int newTest2)
	{
		test2=newTest2;
	}
	
	public int getTest2()
	{
		return test2;
	}
	
	public void setTest3(int newTest3)
	{
		test3=newTest3;
	}
	
	public int getTest3()
	{
		return test3;
	}
	
	public void setAverage(double newAverage)
	{
		average=newAverage;
	}
	
	public double getAverage()
	{
		return average;
	}
	
	public void setGrade(String newGrade)
	{
		grade=newGrade;
	}
	
	public String getGrade()
	{
		return grade;
	}
	
	public double averageGrades() // this mehod just calculates the test grade for each person
	{
		double average=(test1+test2+test3)/3;
		return average;
	}
	
	public String grades() // this method finds the letter grade for each person
	{
		if(average >=90)	
			grade="A";
		else if(average >=80)
			grade="B";
		else if(average >=70)
			grade="C";
		else
			grade="F";
			
		return grade;
	}
	

	public boolean readFromFile(BufferedReader inputFile)throws IOException { //reads all data from file
		String str;
	
		name=inputFile.readLine();
		if (name!=null){
			str=inputFile.readLine();
			test1=Integer.parseInt(str);
		
			str=inputFile.readLine();
			test2=Integer.parseInt(str);
		
			str=inputFile.readLine();
			test3=Integer.parseInt(str);
			
			return true;
		}
		return false;	
	}
		
	public void print() // prints  all items from file to the screen
	{
		System.out.println(name);	
		System.out.println(test1);
		System.out.println(test2);
		System.out.println(test3);
	}		
}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2