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