0

New to Java - I am trying to import a .txt file containing integers. Eventually I want to import the integers into an arraylist and then create a frequency distribution and calculate the average.

Currently I am struggling to use the file dialogue box which is my end goal. I can import the .txt using the file path in my code shown. If any one could help me out with how to use the dialogue box instead it would be greatly appreciated!

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

public class Distribution {

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);

        System.out.println("Enter a complete file path to file you would like to open:");
        String fileName = input.nextLine();

        File inFile = new File(fileName);

        FileReader ins = null;

        try {
            ins = new FileReader(inFile);
            int ch;
            while ((ch = ins.read()) != -1) {
                System.out.print((char) ch);
            }
        } 
        catch (Exception e) {
            System.out.println(e);
        } 
        finally {
            try {
                ins.close();
            } 
            catch (Exception e) {
            }
        }

    } // main end
}
tommybee
  • 2,409
  • 1
  • 20
  • 23
Ekko
  • 43
  • 7
  • Just read the javadocs for https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html - there is even an example - **NB** `chooser.showOpenDialog(parent);` `parent` can be null – Scary Wombat Apr 11 '18 at 02:27

1 Answers1

0

Well, It's simple and easy.

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

class DistributionUI {

    public static void main(String[] args) {
        System.out.println("Enter a complete file path to file you would like to open:");

        final JFileChooser fchooser = new JFileChooser() {

            private static final long serialVersionUID = 1L;

            public void approveSelection() {
                File inFile = getSelectedFile();

                if (inFile.exists() ) {
                    FileReader ins = null;

                    try {
                        ins = new FileReader(inFile);
                        int ch;
                        while ((ch = ins.read()) != -1) {
                            System.out.print((char) ch);
                        }
                    } catch (Exception e) {
                        System.out.println(e);
                    } finally {
                        try {
                            ins.close();
                        } catch (Exception e) {
                        }
                    }
                }
                super.approveSelection();
            }
        };
        fchooser.setCurrentDirectory(new File("."));
        fchooser.setAcceptAllFileFilterUsed(false);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("my file", "txt");
        fchooser.addChoosableFileFilter(filter);
        fchooser.showOpenDialog(null);


    } // main end
}
tommybee
  • 2,409
  • 1
  • 20
  • 23
  • I apologize for not getting back sooner! Had to go to work today... 2 questions. 1) Why do you need "private static final long serialVersionUID = 1L;"? 2) What is "FileNameExtensionFilter filter = new FileNameExtensionFilter("my file", "txt");" doing? – Ekko Apr 11 '18 at 18:24
  • 1) One of interface of the class is a Serializable interface. check another answer. https://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa 2) It means that only txt suffix accepted by filtering. – tommybee Apr 12 '18 at 00:19