this is my first Java course and i am having troubles doing one of the assignments... well i am suppose to read a file containing grades of students...here is an example

034-23-8901 45 78 85 34
342-67-1231 98 45 67 45
452-57-2351 49 78 61 52

the first 9 digits are the social security of the student and the other numbers are the grades. well i am suppose to open this file ( it is name Grades.txt) somehow i am suppose to read the grades and find the average if the student has an average of 90 or above he will be excluded from the final and then my program has to write an outFile named Exemption.txt... it seems very easy but i just can't get it to work...

My question is do i have to use the tokenizer to separate the numbers... if it is possible how can i do it?

Please i will appreciate any help...

this is my first Java course and i am having troubles doing one of the assignments... well i am suppose to read a file containing grades of students...here is an example

034-23-8901 45 78 85 34
342-67-1231 98 45 67 45
452-57-2351 49 78 61 52

the first 9 digits are the social security of the student and the other numbers are the grades. well i am suppose to open this file ( it is name Grades.txt) somehow i am suppose to read the grades and find the average if the student has an average of 90 or above he will be excluded from the final and then my program has to write an outFile named Exemption.txt... it seems very easy but i just can't get it to work...

My question is do i have to use the tokenizer to separate the numbers... if it is possible how can i do it?

Please i will appreciate any help...

Hi,
well there are couple of ways you can do this. String Tokenizer is simplest for parsing. Once you have read the file using io API , read the line into a String buffer. Apply StringTokenizer to extract SSN and the grades.
Average out the grades and write the SSN on Exemptions if the avergae is less.

Hope this helps you. In case you need more help mail me.

cheers,
aj.wh.ca

Better use String.split() instead of StringTokenizer. It will give you an array of Strings split on whatever you give it to use as a field separator.
As your data structure is pretty static you can then use that array and average all the data in it (while skipping the first of course) after converting the Strings to numbers.

See the API docs for details about String.split() :)

jwenting, thanks man i was looking for a better (and easier) way to do it and thanks to you i found it...

I was having troubles using the tokens, every time i was using the separators of ...(line, "-") to take apart the SSN it didn't only take the SSN number but it was taking the whole line... it was kind of frustrating...

i will try using the string.split() right away...

P.S. were are the API docs for details about String.split()? are they located in this forum? ( i'll take a look using google...)

they're located at http://java.sun.com where you can either view them online or download them to install on your computer.
You can also get the JDK there and loads of other goodies (like tutorials, online books, etc.).

ok i have been trying to use the string.split() and so far i can't do it...( keep getting this error messages...) well i have found 2 ways that i think i can do it:

1) This one i found it on my book but it only uses a preditermine string i can't find how to make it read from my grades.txt file:

    double avg;
    double[] numbers = {87, 90, 88, 80, 90};

    double total = 0;
    for (int i = 0; i < numbers.length; i++)
        total += numbers[i];
    avg = total/numbers.length;
    System.out.println(total);
    System.out.println(avg);

With this code i get the average which is something good, but how can i change the code so i can read the numbers from my file??

2) In this one i use a substring but again i don't have an example in how to change the code so i can read it from my Grades.txt file:

    String fullnumber = "234-23-1234 56 67 45 90";
    String social = fullnumber.substring(0, 10);
    String grades = fullnumber.substring(12, 23);
    System.out.println(grades);
    System.out.println(social);

** i can combine Code (1) with code (2) to get the program i am looking for but my problem is that i haven't found how to make read from the file**

3) this is the code i got so far but i can only read the social number... i can't read the rest of the number or even find the average...:

    String line, fileIn = "Grades.txt", fileOut = "Exempt.txt";
    StringTokenizer tokens;
    String social;
    String scores;
    double average;

    FileReader fr = new FileReader(fileIn);
    BufferedReader inFile = new BufferedReader (fr);

    FileWriter fw = new FileWriter (fileOut);
    BufferedWriter bw = new BufferedWriter (fw);
    PrintWriter outFile = new PrintWriter (bw);

    line = inFile.readLine();
    while (line != null)
        {
        tokens = new StringTokenizer (line);
        social = null;

        if (tokens.hasMoreTokens())
            social = tokens.nextToken(",");
        System.out.println(social);                     

        line = inFile.readLine();
        }
    inFile.close();

Can anyone tell me what i am doing wrong or even how can i read a file using Code (1) or Code (2)???

P.S. i will keep looking for more information in java.sun and also on the internet but i will appreciate any hint...

*** Is it possible using a Two-Dimensional Array???***

ok i have been trying to use the string.split() and so far i can't do it...( keep getting this error messages...) well i have found 2 ways that i think i can do it:

1) This one i found it on my book but it only uses a preditermine string i can't find how to make it read from my grades.txt file:

    double avg;
    double[] numbers = {87, 90, 88, 80, 90};

    double total = 0;
    for (int i = 0; i < numbers.length; i++)
        total += numbers[i];
    avg = total/numbers.length;
    System.out.println(total);
    System.out.println(avg);

With this code i get the average which is something good, but how can i change the code so i can read the numbers from my file??

2) In this one i use a substring but again i don't have an example in how to change the code so i can read it from my Grades.txt file:

    String fullnumber = "234-23-1234 56 67 45 90";
    String social = fullnumber.substring(0, 10);
    String grades = fullnumber.substring(12, 23);
    System.out.println(grades);
    System.out.println(social);

** i can combine Code (1) with code (2) to get the program i am looking for but my problem is that i haven't found how to make read from the file**

3) this is the code i got so far but i can only read the social number... i can't read the rest of the number or even find the average...:

    String line, fileIn = "Grades.txt", fileOut = "Exempt.txt";
    StringTokenizer tokens;
    String social;
    String scores;
    double average;

    FileReader fr = new FileReader(fileIn);
    BufferedReader inFile = new BufferedReader (fr);

    FileWriter fw = new FileWriter (fileOut);
    BufferedWriter bw = new BufferedWriter (fw);
    PrintWriter outFile = new PrintWriter (bw);

    line = inFile.readLine();
    while (line != null)
        {
        tokens = new StringTokenizer (line);
        social = null;

        if (tokens.hasMoreTokens())
            social = tokens.nextToken(",");
        System.out.println(social);                     

        line = inFile.readLine();
        }
    inFile.close();

Can anyone tell me what i am doing wrong or even how can i read a file using Code (1) or Code (2)???

P.S. i will keep looking for more information in java.sun and also on the internet but i will appreciate any hint...

** Is it possible using a Two-Dimensional Array???**

Hi,

here is the partial program for you. This one reads from the file and prints the average. Now you can simple check the average and write the file.

import java.io.*;
import java.util.*;
public class Main
{

    String line, fileIn = "Grades.txt", fileOut = "Exempt.txt";
    StringTokenizer tokens;
    String social;
    String scores;
    double average;

    public void calculate()
    {
        try
        {
            FileReader fr = new FileReader(fileIn);
            BufferedReader inFile = new BufferedReader (fr);

            FileWriter fw = new FileWriter (fileOut);
            BufferedWriter bw = new BufferedWriter (fw);
            PrintWriter outFile = new PrintWriter (bw);

            line = inFile.readLine();
            while (line != null)
            {
                tokens = new StringTokenizer (line);
                social = null;

                if (tokens.hasMoreTokens()) social = tokens.nextToken();
                else {System.out.println("File Data error");System.exit(1);}
                average = 0 ;
                for(int i=1;i<5;++i)
                {
                    if (tokens.hasMoreTokens()) scores = tokens.nextToken();
                    else {System.out.println("File Data error");System.exit(1);}
                    average += Integer.parseInt(scores);
                }
                average /= 4;
                System.out.println(average); 

                line = inFile.readLine();
            }
            inFile.close();     
        }catch(IOException e){System.out.println("IO exception");System.exit(1);}
    }
    public static void main(String args[])
    {
        new Main().calculate();

    }
}

if you still face any issues lemme know.
cheers,
aj.wh.ca

Thanks man, i just have one last question:

I can't create the "Exempt.txt" file, i have been using my notes and the book and it should be something like this:

for(int i=1;i<5;++i)
{
if (tokens.hasMoreTokens()) scores = tokens.nextToken();
else {System.out.println("File Data error");System.exit(1);}
average += Integer.parseInt(scores);
}
average /= 4;
if (average > 90)
System.out.println(average);

outFile.print(social + " ");
outFile.print(average + " ");

line = inFile.readLine();

According to my book it should go above the last "line = inFile.readLine();"
it does create the file but is empty, am i doing something wrong??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.