Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing a file  RSS feed

 
Jared Upton
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to parse a text file grades.txt and pull numbers from it to calculate my grade. The grades.txt file will look like this also my current code is below it albeit still a bit rough. Just trying to pulling the info from the file at this point then I will worry about calculating the grade. So any sample code I could look at to see how it is done would be appreciated.





50
80
99
40
85
98
76











import java.util.*; // for the Scanner class
import java.io.*; // for the File class


public class gradecalc {


public void grades() {
try{
File grades = new File("grades.txt");
fileReader = new FileReader(grades);
BufferedReader = new BufferedReader();
} catch(Exception ex) {
ex.printStackTrace();
}
}


public void parseFile() {

try {
// Temporary variables to hold individual field values for a record
double ratscore = 0.0;
double programscore = 0.0;
double examscore = 0.0;
double teamratscore = 0.0;
double teamXMLscore = 0.0;
double teamexamscore = 0.0;
double peerevalscore = 0.0;
[ April 14, 2006: Message edited by: Jared Upton ]
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi


in pseudo code it would look like this:

- readline
- convert string to double
- assign to ratscore
- readline convert to double
- assign to ...


so you need to know how you can read a line from a buffer and how to convert the String to double.

i guess you know how to read a line or you'll find out by looking at the API. conversion can be done trough the Double.parseDouble(String s) method (by the way: parsing a int is done trough Integer.parseInt()... and longs you cna parse trough ... well you found out by now :-) )

and another hint:
if you store the vales in a double[] then the read method will be much simpler:


like this the ratscore is at index 0 of the array, the programscore at index 1 and so on...

of course you need to take care for cases where there are not enough numbers in the file and so on.



hope that was not more than you asked for :-)
 
Jared Upton
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still lost on how to exactly use the Double.parseDouble command and how to get it to read each line and assign it to the array. Here is what I got so far. Any I code I could look at for ideas or help would be appreciated. I am having such a hard time grasping this object oriented language.

import java.util.*; // for the Scanner class
import java.io.*; // for the File class


public class gradecalc {

double[] grades = new double[6];

public void readgrades() {
try {
File getty = new File("grades.txt");
FileReader fr = new FileReader(grades);
BufferedReader br = new BufferedReader(fr);
} catch(Exception ex) {
ex.printStackTrace();
}







public void parseFile() {

double ratscore = 0.0;
double programscore = 0.0;
double examscore = 0.0;
double teamratscore = 0.0;
double teamXMLscore = 0.0;
double teamexamscore = 0.0;
double peerevalscore = 0.0;


for (int i = 0; i < grades.length; i++) {
double[i] grades = new double[i];
[ April 14, 2006: Message edited by: Jared Upton ]
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Jared

try to break the problem into smaller pieces. write some smaller programs first. then in the end write the "big" program.

your problem, as far as i can tell, consists of:
- reading contents of a file
- converting this content to double numbers (line by line)
- do some mangling with the numbers

(the following code will probably not compile...i did not test it. but it should get you started)
try reading the content of a file first:


some of these methods might throw an IOException.


now you should be able to read a file and print its content to the console.


second step is to parse the lines and convert them to doubles and store them at a specified position in an array. here is part of it, i leave it to you how you can combine the part above with this one

this assigns the double 456.34 to the all the indexes in the array.... not exactly what you want but it shows how to loop and how to convert.

when you have combined the two methods and everything works out fine, then try it with a grades file that contains less lines than the grades[] size. what happens ? why ? what do you do about this ? how can you prevent it ?

pascal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!