0

First, I'd like to thank this community, although I haven't been here long, I appreciate every bit of it.

I'm working on a school project, and I think for the most part I got it all figured out except this last little bit. Of course, feel free to offer any advice on anything you see, but my main question is below.

I'm having difficulty printing this out: Should I use a loop, like a boolean to test if true to the posted values?

Count of investment performance indicators: a. Number of As: values $1250 or higher b. Number of Bs: values $1100 or higher and less than $1500 c. Number of Cs: values $900 or higher and less than $1100 d. Number of Ds: values $750 or higher, and less than $900 e. Number of Fs: values less than $750

Lastly, once again thank you all for the patience and understanding. I'm really interested in learning how it all works, so if you could please explain how it works, I'd really appreciate that.

The program calls for a min, max, and mean of the values from a text file. Just a little info to help.(which I've done already)

So far I've tried loops to test the values, I'm not sure I did it right, but it didn't work for me.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Prog07_InvestmentManager {

    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);    
    Scanner inFile=null;;
    File file=null;
    boolean flag=true;
    while(flag) {
        try {
            flag=false;
            System.out.println("Please enter the file name to import, including file extension."); //prompt user
            file = new File(in.nextLine()); //store user input in variable
            inFile = new Scanner(new FileReader(file)); //read file
        } 
        catch (FileNotFoundException e1) {
            flag=true;
        }
    }

    double min = Integer.MAX_VALUE;
    double max = 0;
    double mean = 0;
    inFile.nextLine();

    double line = 0;
    int sum = 0;
    int count = 0;

    while (inFile.hasNextLine()) {

        line=inFile.nextDouble();
        sum+=line;
        count++;

        if(line>max)
            max=line;

        if(line<min)
            min=line;
    }
    mean = (sum/count);
    System.out.printf("Max: %-4.2f\n", max);
    System.out.printf("Min: %-4.2f\n", min);
    System.out.printf("Mean: %-4.2f\n", mean);
    System.out.println();

    if (in.hasNextDouble()) {

    double[] values = new double [(int) in.nextDouble()];

    }

    try {
        Scanner inputFile = new Scanner(file);

        double[] arr = new double[(int) in.nextDouble()];

        for (int i = 0; in.hasNextDouble(); i++) {
            arr[i] = in.nextDouble();
        }

    } catch (FileNotFoundException e) {
        file = new File("investments.txt");
        System.out.print("File not found.\nPlease enter the correct path if needed.");
        file = new File(in.nextLine());                 
        }
    in.close();
    }
}
Josh
  • 41
  • 8
  • 1
    Can you attach a sample file or sample input data? – Vinayak Dornala Apr 03 '19 at 19:28
  • What do you mean? Something like: if (max >= 1250) { do stuff? } – Josh Apr 03 '19 at 19:36
  • 1
    The requirements are not clear. BTW there is a better option to get Statistic info. List inputList = new ArrayList(); while (inFile.hasNextLine()) { line=inFile.nextDouble(); inputList.add(line); } DoubleSummaryStatistics stats = inputList .stream() .mapToDouble(Double::doubleValue) .summaryStatistics(); System.out.println("average: " + stats.getAverage()); – Vinayak Dornala Apr 03 '19 at 19:40
  • I appreciate that, unfortunately we haven't covered array list yet so I can't use it for this. I'm sorry it's not so clear, what I'm basically trying to do is print out how many A's there are (values over 1250), and how many B's (values 1100+, but less than 1500) etc. – Josh Apr 03 '19 at 19:43

1 Answers1

1

If you are just looking to print the frequency of each category(A, B, ...), I would probably create a frequency map using java's HashMap. You can assign keys for each category and then assign values to each key for each occurence of a value. You can get pretty fancy with Java 8+

You might do something like this:

String key;
HashMap<String, Integer> freqMap = new HashMap<>();

while(inFile.hasNextLine()) {
    if(inFile.hasNextDouble()) {
        Double nextDouble = inFile.nextDouble();
        if(nextDouble >= 1250.0) {
            key = A;
            //either create a new key in the HashMap for A, B, etc. or increment the  
            //value associated with the existing key by one
            freqMap.merge(key, 1, Integer::sum);  
        }
        //we are going to use separate if statements as it appears that values can be in 
        //more than one category
        if(nextDouble < 1500.0 && nextDouble >= 1100.0) {
            key = B;
            freqMap.merge(key, 1, Integer::sum);  
        }
        and so on...
    }
}

Finally you will just loop through your HashMap and print both keys and values. Let me know if you need more detail.

Edit: You don't need your array structure for this method. However if you what to iterate through your array to build the map rather than receive data from your file, change the while loop to a for loop iterating through your array.

brandonx
  • 228
  • 2
  • 13
  • Wow! We haven't covered HashMap yet, this is really cool. So this steps line by line in the program for each if statement until end? Then if the value is 1250 or higher it stores it in A? Just trying to understand it, sorry to bug you. – Josh Apr 03 '19 at 19:47
  • 1
    I added some clarity at the end, but with a while loop or for loop we want to iterate through our inFile or alternatively our array and build a frequency map using the hashmap structure. Then we can just print the contents of the frequency map. HashMap is built for problems like this. Using if's instead of if-else if ensures that if an array entry can be included in more than one category that it is tallyed in the map for each cat. it belongs in. – brandonx Apr 03 '19 at 19:51
  • Also this doesn't store the actual values in the map, just how how times a value meets the criteria for a given category. That way you can print A = 5, B = 3 etc. – brandonx Apr 03 '19 at 19:59
  • This is great, the only thing I don't understand is: Hashmap freqMap(); is throwing a syntax error, since this is the first time I'm seeing this I'm not sure why. – Josh Apr 03 '19 at 20:03
  • How do you print from a HashMap? Is it print(freqMap)? I can't seem to get it to work. – Josh Apr 03 '19 at 20:54
  • 1
    Here is a great explanation of how to print keys and values in a map: https://stackoverflow.com/a/5920157 – brandonx Apr 03 '19 at 21:00