0

I need to find the min, max, and average of the balances. I have done this before using for loops, but never with a while loop. Is there a way to pull the min max and average straight from the array without interfering with the while loop perhaps?

10
Helene 1000
Jordan 755
Eve 2500
Ken 80
Andrew 999
David 1743
Amy 12
Sean 98
Patrick 7
Joy 14

where 10 is the number of accounts

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

public class bankaccountmain {

public static void main(String[] args) throws FileNotFoundException {
    Scanner inFile = null;
    try {
        inFile = new Scanner(new File("account.txt"));
        ;
    } catch (FileNotFoundException e) {
        System.out.println("File not found!");

        System.exit(0);
    }
    int count = 0;
    int accounts = inFile.nextInt();
    String[] names = new String[accounts];
    int[] balance = new int[accounts];
    while (inFile.hasNextInt()) {
        inFile.next();
        names[count] = inFile.next();
        inFile.nextInt();
        balance[count] = inFile.nextInt();
        count++;
    }
    System.out.println(Arrays.toString(names));
    System.out.println(Arrays.toString(balance));
    }
}
3
  • So what problem have you encountered? Commented Apr 25, 2014 at 21:21
  • 3
    Uh...there is no difference in how you do this with a while loop and a for loop; You set up variables outside the loop for min, max, total, and the count. You loop around your inFile thing updating the min, max, total, and count. When you exit the loop you have min, max, and the average is total / count. What is your question? Commented Apr 25, 2014 at 21:21
  • In Java 8, using a collection and the stream API, you could do it without a loop, but I am not sure as to what your exact question is, and neither do I really understand what your code does, or what it is supposed to do. Commented Apr 25, 2014 at 21:24

2 Answers 2

2

You are already pulling the values out of the file when you store them in balance[count]. You can then use those values you have just read to do your calculations:

    int min = Integer.MAX_VALUE;
    int max = 0;
    int total = 0;
    while (inFile.hasNext()) {
        names[count] = inFile.next();
        balance[count] = inFile.nextInt();  // balance[count] now is storing the value from the file

        if (balance[count] < min) {  // so I can use it like any other int
            min = balance[count];
        }
        if (balance[count] > max) {  // like this
            max = balance[count];
        }
        total += balance[count]; // and this
        count++;
    }
    double avg = (double)total/count;
Sign up to request clarification or add additional context in comments.

Comments

0

The answer of azurefrog is correct and acceptable (+1).

But I personally prefer to avoid "mixing" too much functionality. In this case: I think that

  • reading the values from a file and
  • computing the min/max/avg

should be separate operations.

Computing the min/max/average of an int[] array is a very common operation. So common that it probably already has been implemented thousands of times (and it's a pity that this functionality was not available in the standard API before Java 8).

However, if I had to implement something like this (and was not willing or allowed to use a sophisticated library solution like, for example, the SummaryStatistics of Apache Commons Math), I'd go for some utility methods:

static int min(int array[]) 
{
    int result = Integer.MAX_VALUE;
    for (int a : array) result = Math.min(result, a);
    return result;
}

int max(int array[]) 
{
    int result = Integer.MIN_VALUE;
    for (int a : array) result = Math.max(result, a);
    return result;
}

static int sum(int array[]) 
{
    int result = 0;
    for (int a : array) result += a;
    return result;
}

static double average(int array[]) 
{
    return (double)sum(array)/array.length;
}

Another advantage (apart from the "purists" argument of "Separation Of Concerns") is that these methods are reusable. You can put them into a IntArrays utility class, and whenever you need this functionality, you can just call these methods.

If you can already use Java 8, then it's even simpler, because exactly these operations now (finally!) are part of the standard API. They are offered in the IntStream class, that exactly has the min(), max() and average() methods that you need. There you could just write something like

int min = IntStream.of(balance).min().getAsInt();
int max = IntStream.of(balance).max().getAsInt();
double average = IntStream.of(balance).average().getAsDouble();

2 Comments

You need to read the array three time in this case while azurefrog only read it once
@Lordofdark I mentioned that the array should only be read once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.