6 Replies - 1599 Views - Last Post: 15 May 2011 - 05:49 AM Rate Topic: -----

#1 Moistmess   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-May 11

Problem with input and output files and sum

Posted 14 May 2011 - 09:07 PM

This program is supposed to read various numbers from a .txt file, then compute the median, mode, mean, max, min, and sum, then write out the answers into an output file. I've tried this problem differently and was able to give an input file, but when entering the output file, it will give me an array out of bounds exception... currently I needed to add the sum of the numbers but it doesn't seem like it wants to work. I've gotten pretty far I think, this is just the final roadblock and I'm stuck. I'm a first year java student and I dont have enough experience to figure this out alone. Suggestions and ideas will be greatly appreciated, I don't expect anyone to write out the code for me, I wanna learn.

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

public class IntegerProcessor
{
 public static void main(String[] args)
  throws FileNotFoundException 
 {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter a File with .txt extension:");
  String fileName = input.nextLine();
  
  Scanner console = new Scanner(new File(fileName));
  int totalNumbers = console.nextInt();
  double[] filearray;
  filearray = new double[totalNumbers];
  
  for (int i = 0; i < totalNumbers; i++)
   filearray[i] = console.nextDouble();
  
  System.out.print("Type the output .txt file:");
  String s = input.next();
  System.out.print("There are " + totalNumbers + " numbers in your input file");
  if (s.length() == 0)
  {
   printResults(filearray);
  } 
  else 
  {
   writeResultsToFile(filearray);
  }
  Arrays.sort(filearray);
  }
 
 
 public static double calculateMax(double[] filearray)
 {
  double maximum = filearray[0];
  for (int i = 1; i < filearray.length; i++)
  {
   if (filearray[i] > maximum)
   {
    maximum = filearray[i];
    }
   }
   return maximum;
 }

 public static double calculateMin(double[] filearray)
 {
  double minimum = filearray[0];
   for (int i = 1; i < filearray.length; i++)
   {
    if (filearray[i] < minimum)
    {
     minimum = filearray[i];
    }
   }
    return minimum;
 }  


 public static double calculateMean(double[] filearray)
 {
  double sum = 0;
   //calculates sum
  for (int i = 0; i < filearray.length; i++)
  {
   sum = sum + filearray[i];
   return sum;
  }
   //Finding the Mean
   double mean = (sum / filearray.length);
   return mean;
 }
 //Finding the Median
 public static double calculateMedian(double[] filearray)
 {
    int middle = (filearray.length+1)/2;
    if (filearray.length%2 == 1) 
    {
        // Odd number of numbers -- return the middle one.
        return filearray[middle];
    } 
    else 
    {
       // Even number -- return average of middle two
       // Must cast to double before division.
       return (filearray[middle-1] + filearray[middle]) / 2.0;
    }
 }
 //Finding the Mode
 public static double calculateMode(double[] filearray)
 {
  double maxValue = -1;
  int maxCount = 0;
  for (int i = 0; i < filearray.length; i++)
  {
   //counts number of times number is in array
   int count = 0;
   for(int j = 0; j < filearray.length; j++)
   {
    if(filearray[j] == filearray[i]){
     count++;
    }
   }
   //Finding the Max Value
   if (count > maxCount)
   {
    maxValue = filearray[i];
    maxCount = count;
   }
  }
 return maxValue;
 }
 
 public static void printResults(double[] filearray)
 {
  double maximumOfArray = calculateMax(filearray);
  double minimumOfArray = calculateMin(filearray);
  double modeOfArray = calculateMode(filearray);
  double meanOfArray = calculateMean(filearray);
  double medianOfArray = calculateMedian(filearray);
  double sumOfArray = calculateSum(filearray[i]);
  System.out.println("Max: " + maximumOfArray);
  System.out.println("Min: " + minimumOfArray);
  System.out.println("Mode: " + modeOfArray);
  System.out.println("Mean: " + meanOfArray);
  System.out.println("Median " + medianOfArray);
  System.out.println("Sum: " + sumOfArray);
 }
 
 public static void writeResultsToFile(double[] filearray)
  throws FileNotFoundException
 {
  PrintStream output = new PrintStream(new File("results.txt"));
  double maximumOfArray = calculateMax(filearray);
  double minimumOfArray = calculateMin(filearray);
  double modeOfArray = calculateMode(filearray);
  double meanOfArray = calculateMean(filearray);
  double medianOfArray = calculateMedian(filearray);
  double sumOfArray = 
    calculateSum(filearray[i]);
  output.println("Max: " + maximumOfArray);
  output.println("Min: " + minimumOfArray);
  output.println("Mode: " + modeOfArray);
  output.println("Mean: " + meanOfArray);
  output.println("Median " + medianOfArray);
  output.println("Sum: " + sumOfArray);
 }
 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with input and output files and sum

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Problem with input and output files and sum

Posted 15 May 2011 - 12:13 AM

Please post the exact error message (cut and paste), point to the code causing the error if it's not obvious from the error message, and describe any other undesirable behavior you'd like help to fix.

In lines 124 and 143, what's i?

This post has been edited by GregBrannon: 15 May 2011 - 12:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 Moistmess   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-May 11

Re: Problem with input and output files and sum

Posted 15 May 2011 - 02:18 AM

View PostGregBrannon, on 15 May 2011 - 12:13 AM, said:

Please post the exact error message (cut and paste), point to the code causing the error if it's not obvious from the error message, and describe any other undesirable behavior you'd like help to fix.

In lines 124 and 143, what's i?


java.lang.ArrayIndexOutOfBoundsException: 1
	at IntegerProcessor.calculateMedian(IntegerProcessor.java:82)
	at IntegerProcessor.writeResultsToFile(IntegerProcessor.java:154)
	at IntegerProcessor.main(IntegerProcessor.java:30)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)



Honestly I'm not sure whats causing the problem, first of all, I'm unable to compute the sum and the [i] was something I tried, I forgot to take it out when I posted :P sorry. I can input the first .txt file, then when I put the output file I get this error message. Its really frustrating because I really want read the numbers off of a file, but when I create a file the program also tells me that there is only 1 number in the file, which there isnt...
Was This Post Helpful? 0
  • +
  • -

#4 darek9576   User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: Problem with input and output files and sum

Posted 15 May 2011 - 02:43 AM

I would google what ArrayOutOfBoundsException means and then try to find the error in the code.
Was This Post Helpful? 0
  • +
  • -

#5 sakshamkum   User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Problem with input and output files and sum

Posted 15 May 2011 - 02:58 AM

your code seems to check the first line of the file as an Integer which is read. you need to make sure that the first number of file represents the total number of int variables in it. somewhere in your calculateMedian() you are having a problem when you ask for one before than the middle value in your array

This post has been edited by sakshamkum: 15 May 2011 - 03:00 AM

Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Problem with input and output files and sum

Posted 15 May 2011 - 03:19 AM

The error message tells you that you have an array index out of bounds, and that the offending index is 1. I'm guessing you're thinking your arrays have more items in them than zero. In fact, how many arrays do you have? One? Where is it created? How could it be created so that it contains only 0 (< 1) members?

Hint: Look at line 16 and figure out why totalNumbers = 0.
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Problem with input and output files and sum

Posted 15 May 2011 - 05:49 AM

Cut & paste problem :)

You exit your median method here at the first iteration of the loop returning the value in filearray[0]

 for (int i = 0; i < filearray.length; i++)  
 {  
   sum = sum + filearray[i];  
   return sum;  // <----------   ???
 }  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1