Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program compile but once i click on text it does not do anything? what am i miss

ID: 3651205 • Letter: P

Question

Program compile but once i click on text it does not do anything? what am i missing?
need to add ask user input (look for first file) and also a user output (look for second file) ?


import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.Scanner.*;
import javax.swing.border.*;
import java.util.StringTokenizer.*;

public class FileChooserGUI extends JFrame implements ActionListener
{
private JButton button1;
private JPanel panel;
JLabel label1, label2, label3;
String output;
int size;
int wordCount = 0;

private void FileChooserGUI(){ //Constructor
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());

label1 = new JLabel("the number of Words: "); //1st label
label2 = new JLabel("the number of Lines: "); //2nd label
label3 = new JLabel("Target search Word: "); //3rd label
button1 = new JButton("START");
button1.addActionListener(this);

window.add(label1); //adding three labels to the window frame
window.add(label2); //adding button 2
window.add(label3); //adding button 3
window.add(button1); //adding 1 button to window frame

panel = new JPanel();
panel.setPreferredSize(new Dimension(300,200));
window.add(panel); //adding panel to window
setVisible(true);
pack();

label1.setText("File Name is : " + output);
label2.setText("File Size is : " +Long.toString(size));
label3.setText("word target : " +Integer.toString(wordCount));
label1.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
}

public static void main(){ //(String args[])//throws Exception{
JFileChooser chooser = new JFileChooser();
int code = chooser.showOpenDialog(null);
if (code == JFileChooser.APPROVE_OPTION)
try {
File selectedFile = chooser.getSelectedFile();
Scanner input = new Scanner(selectedFile);

//--> cannot find variable selectedFile. error
FileReader i= new FileReader(selectedFile);
File output = chooser.getSelectedFile();
FileWriter o = new FileWriter(output);
String out = selectedFile.getName(); //name of file

// try
// (BufferedReader reader = Filenew BufferedReader(selectedFile)){
// String line = null;
// while ((line = reader.readLine()) != null) {
// System.out.println(line);
// }
} catch (IOException output) {
System.err.format("IOException: %s%n", output);
}


//write from o (o file) to i (input file)
//}
//while(i.hasNext())
//{
// o.write(i);
//}

//while loop to count all the words in txt file
}
}

Explanation / Answer

please rate - thanks

your main is missing the parameter, for some reason you commented it out

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.Scanner.*;
import javax.swing.border.*;
import java.util.StringTokenizer.*;

public class FileChooserGUI extends JFrame implements ActionListener
{
private JButton button1;
private JPanel panel;
JLabel label1, label2, label3;
String output;
int size;
int wordCount = 0;

private void FileChooserGUI(){ //Constructor
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());

label1 = new JLabel("the number of Words: "); //1st label
label2 = new JLabel("the number of Lines: "); //2nd label
label3 = new JLabel("Target search Word: "); //3rd label
button1 = new JButton("START");
button1.addActionListener(this);

window.add(label1); //adding three labels to the window frame
window.add(label2); //adding button 2
window.add(label3); //adding button 3
window.add(button1); //adding 1 button to window frame

panel = new JPanel();
panel.setPreferredSize(new Dimension(300,200));
window.add(panel); //adding panel to window
setVisible(true);
pack();

label1.setText("File Name is : " + output);
label2.setText("File Size is : " +Long.toString(size));
label3.setText("word target : " +Integer.toString(wordCount));
label1.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
}

public static void main(String args[]){ //(String args[])//throws Exception{
JFileChooser chooser = new JFileChooser();
int code = chooser.showOpenDialog(null);
if (code == JFileChooser.APPROVE_OPTION)
try {
File selectedFile = chooser.getSelectedFile();
Scanner input = new Scanner(selectedFile);

//--> cannot find variable selectedFile. error
FileReader i= new FileReader(selectedFile);
File output = chooser.getSelectedFile();
FileWriter o = new FileWriter(output);
String out = selectedFile.getName(); //name of file

// try
// (BufferedReader reader = Filenew BufferedReader(selectedFile)){
// String line = null;
// while ((line = reader.readLine()) != null) {
// System.out.println(line);
// }
} catch (IOException output) {
System.err.format("IOException: %s%n", output);
}


//write from o (o file) to i (input file)
//}
//while(i.hasNext())
//{
// o.write(i);
//}

//while loop to count all the words in txt file
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote