0

Do I have to add some exit statement?

Code seems to be running well, but after it finish its task I still can't interact with console.

enter image description here

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package excelb;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;


/**
 *
 * @author hp
 */
public class Excelb {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        String row;
        String row2;
        File fn = null;
        int counter = 0;



        Scanner in = new Scanner(System.in);
        System.out.println("Enter the run identifier for output file");
        String output = in.nextLine();


        //input
        JFileChooser jFileChooser = new JFileChooser();
        jFileChooser.setCurrentDirectory(new File("C:/"));

        int result = jFileChooser.showOpenDialog(new JFrame());


        if (result == JFileChooser.APPROVE_OPTION) {
             fn=jFileChooser.getSelectedFile();
            System.out.println("Data file: " + fn.getAbsolutePath());
            //System.out.println("Data file: " + fn.getTitle());
        }



        BufferedReader csvReader = new BufferedReader(new FileReader("Keywords.csv"));
        BufferedReader csvReader2 = new BufferedReader(new FileReader(fn.getAbsolutePath()));

        row = csvReader.readLine();
        String[] data = row.split(",");
        while (counter < data.length) {
            while ((row2 = csvReader2.readLine()) != null) {
                String[] data2 = row2.split(",");
                for (int j = 0; j < data.length; j++) {
                    if ((data2[data2.length - 1].contains(data[j]))) {
                        //DateFormat df = new SimpleDateFormat("dd/MM/yy");
                        //Date dateobj = new Date();
                        //String d = df.format(dateobj);
                        //d = d.replaceAll("/", "");
                        String newFile = data[j] + "_" + output + ".csv";
                        FileWriter csvWriter = new FileWriter(newFile, true);
                        for (int i = 0; i < data2.length; i++) {
                           csvWriter.append(data2[i]);
                            csvWriter.append(",");
                        }
                        csvWriter.append("\n");
                        csvWriter.close();
                    }
                }
            }
            counter++;
        }
        System.out.println("Finished");
        csvReader.close();
        csvReader2.close();
    }

}
2
  • You need to use a loop to iterate the processing logic
    – Ehab Qadah
    Commented Jun 2, 2020 at 0:58
  • You know, now would be a great time to start using Windows new and improved subsystem for Linux. CMD is the most developed unfriendly command line I know of
    – Adrian M.
    Commented Jun 2, 2020 at 2:20

2 Answers 2

3

Pshemo's answer explains why your app isn't ending.

But it does fail to make the key point, which is:

To exit a Java app, run System.exit(0); - and consider returning a non-0 value if errors occurred. Don't wait for the JVM to exit itself (it does do so, once the only remaining live threads have the daemon flag set) - that's a frail strategy if the point is to close the app. Just call exit.

3
  • Edited my answer to include missing portion. Hope you don't mind.
    – Pshemo
    Commented Jun 2, 2020 at 10:44
  • i put this in and it wouldnt compile
    – Joel Ortiz
    Commented Jun 3, 2020 at 16:21
  • c:\java>javac Excelb.java Excelb.java:102: error: <identifier> expected System.exit(0); ^ Excelb.java:102: error: illegal start of type System.exit(0); ^ 2 errors
    – Joel Ortiz
    Commented Jun 3, 2020 at 18:09
1

Problem:

Your application consists of two separate threads:

  1. handling main method
  2. handling events from swing events.

Even if main thread will end its execution, swing thread is still running and doesn't allow your application to exit.
BTW that is a good thing. We are building GUI components like frames with buttons, labels, etc. in main method. Now, when main method/thread ends, should application be closed and along with it all built GUI? That wouldn't make much sense.

Why problem appeared?

Because you used new JFrame() which started swing thread responsible for handling events related to swing/AWT components (like button clicks, etc.).

Solutions:

Approach 1: Prevent problem from happening

Don't start swing thread with new JFrame() if you are not planning to create application based on swing GUI.

In your case simple solution would be passing null instead of new JFrame() to jFileChooser.showOpenDialog. This would also let that dialog be placed at the center of the screen:

int result = jFileChooser.showOpenDialog(null);

Approach 2: Forceful exit

To forcefully close your application you can use System.exit(0).

But generally it is better to prevent problems from happening than solving them.

3
  • c:\java>javac Excelb.java Excelb.java:102: error: <identifier> expected System.exit(0); ^ Excelb.java:102: error: illegal start of type System.exit(0); ^ 2 errors
    – Joel Ortiz
    Commented Jun 3, 2020 at 18:09
  • Where exactly did you put System.exit(0); in your code? It should be placed at the end of main method (since at this point we want to exit). Anyway have you tried using null instead of new JFrame()? IMO it is preferred solution over System.exit(0); since it prevents problem forom happening, instead of solving problem created by new JFrame().
    – Pshemo
    Commented Jun 3, 2020 at 20:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.