The Artima Developer Community
Sponsored Link

Java Answers Forum
HELP String Tokenizer and Calculations

2 replies on 1 page. Most recent reply: Sep 2, 2002 4:49 AM by Ana

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Debs

Posts: 1
Nickname: debs
Registered: Aug, 2002

HELP String Tokenizer and Calculations Posted: Aug 28, 2002 3:19 AM
Reply to this message Reply
Advertisement
I am doing a first year level JAVA subject and am having problems writing a program. The aim is to implement a program that performs statistical calculations on students records from a text file.
The input text file looks as follows:

s34567 comp1 6 comp4 5 comp3 3
serge comp1 7 comp4 4 comp2 6

... and so on for a few lines.

The first word being the student name, the second being a subject code, followed by a grade, followed by another subject code, followed by a grade, etc.

The program must contain one line for each course in the input, containing the course name, the average course grade and the number of students taking the subject. The output must also contain two lines with the mean and the standard deviation of the course averages. Everything must be rounded to two decimal places.
Example output as follows:

comp1 6.50 2
comp4 4.60 4
comp3 3.67 3
...
overall mean: 5.55
overall standard deviation: 1.38

I know that basically I have to write some code with BufferedReader, Printwriter, StringTokenizer, and stuff but I just don't really know where to start or what to do.
If anyone could help me I would really appreciate it.


Neva Mohammad

Posts: 28
Nickname: neva
Registered: Apr, 2002

Re: HELP String Tokenizer and Calculations Posted: Aug 29, 2002 7:57 AM
Reply to this message Reply
I hope this helps...you need to first write some thing that reads a file and displays it..

import java.io.*;

class readFile(){
public static void main(String [] args) throws IOEXception{
BufferedReader fin = new BufferedReader(new FileReader("x.txt"));

PrintWriter fout = new PrintWriter(new FileWriter("y.txt"));

String s;

for (int i=0;;i++){
s = fin.readLine();
if (s==null) break;
else fout.println(i + " " + s);}
fout.close();
}
}
just play around with this so that you can also read in integers, etc.

Ana

Posts: 12
Nickname: anika
Registered: Aug, 2002

Re: HELP String Tokenizer and Calculations Posted: Sep 2, 2002 4:49 AM
Reply to this message Reply
Hi, this is an example class for one subject (comp1). It could be something based on this for more subjects.I define two static variables, comp1 and grade1 to take account for the number of students following comp1 course, and for the average course grade.

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

class Calculator{

public static int comp1;
public static int grade1;

public static void main(String[] args){
try{
try{
//read from file "students.txt"
BufferedReader br = new BufferedReader(new FileReader("students.txt"));
//write in file "subjects.txt"
PrintWriter pw = new PrintWriter(new FileWriter("subjects.txt"));

String s;

while ((s= br.readLine())!=null){
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreElements()){
if (st.nextToken().equals("comp1")){
if(st.hasMoreElements())
grade1 += Integer.decode(st.nextToken()).intValue();
comp1++;
}

}
}
//we get mean value: If included to avoid division by zero exception when compiling
if(comp1!=0) grade1 = grade1/comp1;
//write results in output file
pw.println("comp1 "+grade1 + " " +comp1);
pw.close();
}
catch (FileNotFoundException nfe){}

}catch(IOException ioe){}

}
}

Flat View: This topic has 2 replies on 1 page
Topic: applets-package-images-appletviewer problem !!? Previous Topic   Next Topic Topic: Page Tracking with Sessions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use