Need some Java debugging help (fairly easy I think)

Discussion in 'OT Technology' started by Reign, Nov 17, 2003.

  1. Reign

    Reign Banned

    Joined:
    Nov 17, 2003
    Messages:
    21,304
    Likes Received:
    0
    Location:
    Westminster, CO
    Ok, pretty much got this done. The program takes a user inputed file and puts the data in that file into an array. It then calculates the average value, minimum value, maximum value, and the standard deviation of all the data in tha rray. However, it always prints out "null" and "bad data" from the fileRead method even if the file open was successful. Even though it does print those out, the rest of the program still executes fine and calculates everything correctly.. Can anyone tell me why, because I'm not seeing it. It also says the minimum value is always 0.0 even if there is no 0.0 value in the data file you input. It will say -28 is the lowest if that is in the data file though.

    Thanks for any help anyone can give.

    Code:
    import java.io.*;
    /**ExamStats supports is a value class that supports an
     * array of type double, and methods to perform statistics
     * on the array data.
     */
    public class ExamStats
    {
        private BufferedReader inFile;
        private BufferedReader keyboard;
        private boolean fileOpen;
        private double array[];
        private int actualSize;
        private final static int DEFAULTSIZE=100; 
    
        /**Default constructor - 
         * Creates a double array of DEFAULT size - 
         * sets actualSize to zero - 
         * builds the standard input object keyboard.
         */
        public ExamStats()
        {
    	array = new double[DEFAULTSIZE];
    	actualSize=0;
    	buildStandardIO();
    	fileOpen=false;
        }
    
        /**Array size constructor - 
         * Creates a double array of arraySize - 
         * sets actualSize to zero - 
         * builds the standard input object keyboard.
         */
        public ExamStats(int arraySize)
        {
    	array = new double[arraySize];
    	actualSize=0;
    	buildStandardIO();
    	fileOpen=false;
        }
    
        /**Builds an input stream reader named keyboard.
         *
         */
        private void buildStandardIO()
        {
    	try{
    	    keyboard = new BufferedReader(new InputStreamReader(System.in));
    	}
    	catch(Exception e)
    	    {
    		System.out.println(e);
    		System.exit(1);
    	    }
        }
    
        /**Prints array values to the screen.
         *
         */
        public void printArray()
        {
    	for(int i=0; i<actualSize; i++)
    	    {
    		System.out.println(array[i] + " ");
    		if((i+1)%5 == 0);
    	    }
        }
    
        /**Opens a file for input
         *
         */
        public void openInputFile(String filename) 
        {
    	for(;;){
    	    try{
    		inFile = new BufferedReader( new FileReader(filename) );
    		break;
    	    }//end try
    	    catch(FileNotFoundException e)
    		{
    		    System.out.println(filename + " could not be opened. " +
             " You may enter a new filename or exit the program.\n" +
             "Enter F if you want to enter a new file name.\n" +
    				       "Ender Q if you want to exit the program");
    
    		    try{
    			int unicode = keyboard.read();
    			char answer = (char)unicode;
    			unicode = keyboard.read();   //read newline
    			switch(Character.toUpperCase(answer))
    			    {
    			    case 'Q':
    				System.exit(1);
    			    case 'F':
    				System.out.println("enter new filename ");
    				filename = new String(keyboard.readLine());
    				break;
    			    default:
    				System.out.println("Can't process request.  Goodbye!");
    				System.exit(1);
    			    }//end switch
    		    }//end try
    		    catch(Exception e1)
    			{
    			    System.out.println(e1);
    			    System.exit(1);
    			}
    		}//end catch
    	}//end for
    	System.out.println("File " + filename + " has been opened.");
    	fileOpen = true;
        }//end fileRead
        
        /**Reads data from a file -
         * stores data in array
         */
        public void fileRead()
        {
    	if(fileOpen == false)
    	    {
    		System.out.println("No file has been opened for input.");
    		System.exit(1);
    	    }
    	double temp=0;
    	String hold;
    	actualSize = 0;
    	try{
    	    hold = inFile.readLine();
    	    while(actualSize < array.length && hold.length() != 0)
    		{
    		    temp = Double.parseDouble(hold);
    		    array[actualSize] = temp;
    		    ++actualSize;
    		    hold = inFile.readLine();
    		}//end while
    	}//end try
    	catch(Exception e)
    	    {
            
    		System.out.println(e.getMessage()); 
    		System.out.println("bad data."); 
    	    }
    	System.out.println(actualSize +" values were entered."); 
        }//end method 
    
        /**Prompts for a filename -
         *reads a filename from the keyboard
         */
        public String getInputFileName()
        {
    	String filename = new String();
    	System.out.println("enter input file name");
    	try{
    	    filename = new String(keyboard.readLine());
    	}
    	catch(Exception e)
    	    {
    		System.out.println(e);
    		System.exit(1);
    	    }
    	return filename;
        }
        /**Returns average array value 
         *
         */
        public double averageVal()
        {
    	double avg = 0;
    	double sum = 0;
    	for(int i = 0; i < array.length; ++i)
    	    {
    		sum += array[i];
    	    }
    	return avg = sum/array.length;
        }
    
        /**Returns smallest array value 
         *
         */
        public double minVal()
        {
    	double min = 65536;
    	for(int i = 0; i < array.length; ++i)
    	    {
    		if(array[i] < min)
    		    {
    			min = array[i];
    		    }
    	    }
    	return min;
        }
        /**Returns largest array value 
         *
         */
        public double maxVal()
        { 
            double max = -65536; 
            for(int i = 0; i < array.length; ++i)
                { 
                    if(array[i] > max) 
                        {
                            max = array[i]; 
                        }
                } 
            return max;
        } 
    
        /**Returns the standard deviation 
         *
         */
        public double standardDeviation()
        {
    	double sd = 0;
    	double s = 0;
    	double n = array.length;
    	double avg = 0;
            double sum = 0; 
            for(int j = 0; j < n; ++j)
                { 
                    sum += array[j]; 
                } 
    	avg = sum/n;
    	for(int i=0; i < n - 1; i++)
    	    {
    		s += ((array[i] - avg)*(array[i] - avg));
    	    }
    	return sd = Math.sqrt(s/n);
        }
    
        public static void main(String[]args)
        {
    	ExamStats theclass = new ExamStats();
    	theclass.openInputFile(theclass.getInputFileName());
    	theclass.fileRead();
    	theclass.printArray();
    	double maxVal = theclass.maxVal();
    	double minVal = theclass.minVal();
    	double sdVal = theclass.standardDeviation();
    	double avgVal = theclass.averageVal();
    	System.out.println("The average value of the data is: " +avgVal);
    	System.out.println("The maximum value of the data is: " +maxVal);
    	System.out.println("The minimum value of the data is: " +minVal);
    	System.out.println("The Standard Deviation of the data is: " +sdVal);
        }
    
    }
    
     

Share This Page