简体   繁体   中英

Arrays and read and write text files issue - Java

Here is what I have to do...

  • Input File: dishin.txt

  • Output File: dishout.txt

  • Time Limit: 1 second

You do not like statisticians, and statisticians do not like you. Ever since that life actuary kicked sand into your ice cream when you were four, you have waged a slowly escalating war against those number-crunching jerks. But all that is about to change.

After many sleepless nights you have conceived the ultimate revenge: beating them at their own game. Using your computer programming skills, you will write a freeware statistics package so thorough, so complete, that statisticians all around the world will be out of a job. It will be able to predict weather patterns, calculate life expectancies, and even play the perfect poker game.

First, though, you must implement word wrap. However, this task is rather finnicky, not very mathematical in nature, and ultimately not very important. More urgently, you also need to implement some basic data analysis. Specifically, you decide to write a test program that takes a data set (a list of integers) and calculates the following measures of spread:

  • Minimum - the smallest value in the list. For example, the minimum of the numbers {5, 6, 5, 3} is 3 .

  • Maximum - the largest value in the list. For example, the maximum of the numbers {5, 6, 5, 3} is 6.

  • Mean (or average) - defined as the sum of everything in the list divided by the number of items in the list. For example, the mean of the numbers {5, 6, 5, 3} is (5+6+5+3)/4 = 19/4 = 4.75 . However for simplicity you are asked to round all answers down to the nearest whole number. So the mean of the numbers {5, 6, 3, 3} , rounded down, is 4 .

Input:

The first line of input will consist of a single integer n (1 <= n <= 1,000), the size of your data set. The following n lines will describe the data set. Each of these lines contains an integer between 0 and 1,000,000 inclusive.

Output

The output file should consist of three integers separated by spaces: the minimum, maximum and mean of the data set.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class Solution {

public static void main(String[] args) throws IOException {

    ///home/sebastian/workspace/Informatics Competition/src/

    Scanner input = new Scanner(new File("/home/sebastian/workspace/Informatics Competition/src/dishin.txt"));
    BufferedWriter output = new BufferedWriter(new FileWriter("/home/sebastian/workspace/Informatics Competition/src/dishout.txt"));

    ArrayList<Integer> numbers = new ArrayList<Integer>();

    while(input.hasNextInt()) {
        numbers.add(input.nextInt());
    }

    numbers.remove(0);
    Object max = Collections.max(numbers);
    Object min = Collections.min(numbers);

    System.out.println(numbers);

    int sum = 0;

    for (int i = 0; i<=numbers.size(); i++) {
        sum += (int)(numbers.get(i));

    }

    int mean = (int)(sum / numbers.size());

    output.write(String.valueOf(min+" "+max+" "+mean));

    input.close();
    output.close();

}

}

Here is the error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.rangeCheck(ArrayList.java:638)
    at java.util.ArrayList.get(ArrayList.java:414)
    at Solution.main(Solution.java:35)

It should be

for (int i = 0; i<numbers.size(); i++) 

instead of

for (int i = 0; i<=numbers.size(); i++) 

index must be less than the size of the numbers. Index starts from 0 till (size - 1)

Use The For-Each Loop to avoid such issues if you are interested to iterate each and every item of the array.

For-each loop (Advanced or Enhanced For loop):

The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable

Syntax of for-each loop:

for(data_type variable : array | collection){} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM