Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is my error that I am getting back Enter input file name: data_in.txt Enter

ID: 3625496 • Letter: T

Question

This is my error that I am getting back

Enter input file name: data_in.txt
Enter output file name(enter if screen):
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Statistics2.main(Statistics2.java:31)

This is my code

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

public class Statistics2 {

public static void main(String[] args)
throws FileNotFoundException {
int i = 0, j;

int[]index = new int[10];
int count = 0;
boolean file;
double[]x = new double[1000];
double average,sdd,variance,med;
String filename;
PrintStream output;
System.out.print("Enter input file name: ");
Scanner console = new Scanner(System.in);
filename = console.nextLine();
Scanner input = new Scanner(new File(filename));
System.out.print("Enter output file name" +
"(enter if screen): ");
filename = console.nextLine();
if ((filename.length()) == 0){
output = System.out;
}else{
output = new PrintStream(new File(filename));
}
while(input.hasNextDouble()){
x[count] = input.nextDouble();
count++;
}
sort(x,count);
average = mean(x,count);
med = median(x,count);
j = mode(x,count,index);
variance = calcvariance(x,count,average);
sdd = standardd(variance);
print(output,x,count,average,med,j,index,sdd);
}
public static void print(PrintStream output,
double x[], int count,double average,
double med, int j, int index[], double sdd){
int i;
double max = (x[count -1]);
double min = x[0];
output.println("Your file contains " +
count + " numbers.");
output.printf("Max: %.3f ",(max));
output.printf("Min: %.3f ",(min));
output.printf("Mean: %.3f ",(average));
output.printf("Median: %.3f ",(med));
output.printf("Mode: ");
if(j == -1){
output.println("none");
}
else if(j==0){
output.printf("%.3f ",x[index[0]]);
}else{
output.printf("%.3f ",(x[j]));

}

output.printf("Standard deviation: %.3f ",
(sdd));
}


public static int mode(double x[], int n,
int index[]){
int []ctr = {0,0,0};
double temp = 0;
for(int i =0; i<(n-1); i++){
temp = x[i];
while (temp == x[i]){
i++;
ctr[1]++;
}
if (ctr[1] > ctr[0]){
ctr[0] = ctr[1];
ctr[1] = 0;
i--;
ctr[2] = i;
}
ctr[1]=0;
}
return(ctr[2]);
}

public static double median(double x[],int n){
double mid;
if(n%2 == 1){
mid = x[n/2];
}else{
mid = x[n/2]+x[n/2-1];
mid = mid/2.;
}
return mid;
}

public static double mean(double x[], int n){
double sum = 0;
int i;
for (i = 0; i < n; i ++){
sum += x[i];
}
return (double)sum/n;
}
public static double calc(double x, double avg){
double temp;
temp = x-avg;
return temp*temp;
}
public static double calcvariance(double x[], int n,
double average){
double mean, sum = 0;
int i;
for(i = 0; i < n; i++){
sum+=calc(x[i],average);
}
mean = sum/n;
return mean;
}
public static double standardd(double variance){
return Math.sqrt(variance);
}
public static void sort(double x[],int n){
int i,j;
double t;
for(i = 0; i < n - 1; i++){
for(j=i; j < n; j++){
if(x[i]>x[j]){
t = x[i];
x[i] = x[j];
x[j] = t;
}
}
}
}
}

This is what I need my code to output
/for a file of 8 elements(numbers)

ßœEnter input file name: num.txt
ººßœEnter output file name(enter if screen):
œœßœYour file contains 8 numbers.
œœßœMax: 10.600
œœßœMin: 1.100
œœßœMean: 4.338
œœßœMedian: 3.950
œœßœMode: 1.100
œœßœStandard deviation: 2.874

/for a file of 1000 elements(numbers)

ßœEnter input file name: data_in.txt
ººßœEnter output file name(enter if screen):
œœßœYour file contains 1000 numbers.
œœßœMax: 10.000
œœßœMin: 0.000
œœßœMean: 5.076
œœßœMedian: 5.100
œœßœMode: 4.000
œœßœStandard deviation: 2.819
œœßœ

Explanation / Answer

Just change double[]x = new double[1000]; to double[]x = new double[1001]; and it will work. Your code tries to access x[1001] and Java complains because the array is only initialized to x[1000].

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote