hi i think the answer to this will probably be quite simple as im new to java and have probably missed something quite basic. i want to write a program that takes data from a txt file and calculates the mean and sd. the txt file has two columns of data, one column is vibration and one is viscosity. i keep getting the error message cannot resolve symbol for variable k. i thought i had said what k is. any help would be greatly appreciated. rachael
import java.io.*; import java.util.*;
public class coursework{
static final int x = 150;
public static void main ( String args [] ) {
BufferedReader din; FileReader fin;
File f = new File ("normal.txt"); String[] records = new String[x];
try { fin = new FileReader (f); din = new BufferedReader (fin); for (int k=0; k<x; k++){ records[k] = din.readLine(); } } catch (Exception e){ System.exit(0); } double[] vibration = new double[x]; double[] viscosity = new double[x];
for (int k=0; k<x; k++){ StringTokenizer st = new StringTokenizer(records[k], " ");
for ( k=0; k<x; k++){ sumVibration += Double.parseFloat ( args [k] ) ; } sumVibration / (int) x = mean1; System.out.println ("\n\t The mean of the vibration values is: "+ mean1 );
for ( k=0; k<x; k++){ sumViscosity += Float.parseFloat ( args [k] ); } sumViscosity /x = mean2; System.out.println ("\n\t The mean of the viscosity values is: "+ mean2 ); } }
for ( k=0; k<x; k++){ sumVibration += Double.parseFloat ( args [k] ) ; } sumVibration / (int) x = mean1; System.out.println ("\n\t The mean of the vibration values is: "+ mean1 );
thanks alot singh. I now have no errors when i complile and can get the program to list the numbers from the text file yet the program then crashes and i get the error message Exception in Thread "main" java.lang.ArrayIndexOutOfBoundsException at coursework.main(coursework.java:52)
from searching the net it looks to me that there was a problem with my program as it was trying to find an extra number in the array, due to the loop starting at k=0, therefore i changed line 51 51> for ( k=0; k<x; k++){ 52> sumVibration += Double.parseFloat ( args [k] ) ; to 51> for ( k=0; k<(x-1); k++){ 52> sumVibration += Double.parseFloat ( args [k] ) ;
yet this has not solved the problem id be very greatfull if you could enlighten me as to where i am stuck, i did not realise java could be this frustrating!! thanks rachael
thanks for your reply singh, however i have tried replacing args with records and instead of the ArrayIndexOutOfBoundsException i now get the error message :
Exception in thread "main" java.lang.NumberFormatException: at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal).java:1176) at java.lang.Float.parseFloat(Float.java:183) at problem.main(problem.java:54)
think i might have just made the problem a whole lot more complicated. realise i am completely out of my depth and think i might have to purchase java for dummies tomorrow as the net isnt helping me that much! dont worry if you cant help me, thank you your time.