0

My class project is to prompt the user for a file, that file name they input must be correct, once file is established, the first line of the file is the size of the array. Then, I have to assign each value, line by line, to the array.

Quick note** We haven't learned about buffered reader yet so I can't use that. I also can't use ArrayList, also haven't covered that yet.

Question: How do I make sure the file name they input is right? So far in class we've used while loops to check, but I'm wondering if there's a better way. I need the user to enter "investments.txt" otherwise I need to prompt them again and again. Also any points on improving existing code is very appreciated, I'm very new to this.

Code so far:

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);    

    System.out.println("Please enter the file name to import. Example: Investments.txt."); //prompt user
    File file = new File(in.nextLine());

    try {
        Scanner inFile = new Scanner(new FileReader("investments.txt"));
        double min = Integer.MAX_VALUE;
        double max = 0;
        double mean = 0;
        int num = inFile.nextInt();
        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.println("Max: "+max);
        System.out.println("Min: "+min);
        System.out.println("Mean: "+mean);
        System.out.println();
    } catch (FileNotFoundException e1) {

    }
    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
    Possible duplicate of [Java: Reading a file into an array](https://stackoverflow.com/questions/285712/java-reading-a-file-into-an-array) – AJWGeek Apr 02 '19 at 22:42
  • 1
    Please narrow your question to just one specific part of the code that you’re having problems with – Bohemian Apr 02 '19 at 23:59
  • I've narrowed down my questions to just one, I sincerely apologize for being all over the place with this. – Josh Apr 03 '19 at 15:54

2 Answers2

1

First suggestion is to use the List<> interface, instead of a primitive array

List<Double> value = new ArrayList<>();

1 & 2 Can be done easily by sorting the list, the first and last element will be the min and max respectively.

After that, it is just a matter or using a foreach loop to find the other values

for (Double element : value) {
   // do math
}
  • Thanks for the input! Unfortunately we haven't covered array list in my class yet, so I can't use that. – Josh Apr 03 '19 at 16:51
  • 1
    You can do the same thing in a primitive array, just use Arrays.sort(). and the foreach loop is bascially the same – Chris Stoner Apr 04 '19 at 20:15
1

So there are a couple issues with your code:

  1. After you read in your file name, you should be done with the 'in' scanner for now. You are attempting to start reading your data from System.in which will cause your program to hang unless you manually enter all of your data.

  2. You never overwrite your original file variable with the String from System.in. You don't even necessarily need the default value to begin with. Just remove File file = new File("investments"); and String fileName = in.nextLine();

    Then add File file = new File(in.nextLine()); after your prompt.

  3. Your while loop outside of the try/catch is also problematic and can be deleted completely. Again, you're trying to read all of your data from System.in.

  4. You're mismatching hasNextDouble() and .nextLine(). This works with your current setup because each number is on a new line, but in general you should use the same data type.

  5. In general, you can read in an array of doubles from a file using the following:

   Scanner scanner = new Scanner(file);
   double arr = new double[scanner.nextInt()];

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

Isaac Smith
  • 137
  • 9
  • Thanks, that actually helps a lot. My main problem is getting the the values and sticking them in the array. I guess to be blunt, I'm not understanding how that works, not completely anyway. I understand 'nextLine' takes it line by line, so the way it was explained to me is, as long as I use nextLine correctly I can just use it to assign values to the array, is this right? – Josh Apr 03 '19 at 12:43