Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JTextField;
- import java.io.File;
- import java.util.Scanner;
- import java.awt.event.*;
- import javax.swing.*;
- public class ZadanieNowe extends JFrame implements ActionListener {
- public JButton browser;
- public JTextField enterTextField;
- public JTextField text;
- public static File chosenFile;
- public ZadanieNowe() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(400, 200);
- setTitle("StatystykaPliku");
- text = new JTextField();
- browser = new JButton("Browse File");
- add(browser, BorderLayout.NORTH);
- add(text, BorderLayout.CENTER);
- setVisible(true);
- text.addActionListener(this);
- browser.addActionListener(this);
- }
- public void actionPerformed(ActionEvent event) {
- JFileChooser fc = new JFileChooser();
- if (event.getSource() == browser) {
- if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
- chosenFile = fc.getSelectedFile();
- JOptionPane.showMessageDialog(null, "Chosen File is: "
- + chosenFile.getAbsolutePath());
- }
- }
- }
- public static void main(String[] args) {
- new ZadanieNowe();
- int i = 0;
- int j = 0;
- int k = 0;
- try {
- Scanner in = new Scanner(chosenFile);
- while (in.hasNextLine()) {
- String a = in.nextLine();
- k++;
- char chars[] = a.toCharArray();
- i += chars.length;
- }
- in.close();
- Scanner in2 = new Scanner(chosenFile);
- while (in2.hasNext()) {
- in2.next();
- j++;
- }
- in2.close();
- System.out.println("the number of chars is :" + i);
- System.out.println("the number of words is :" + j);
- System.out.println("the number of lines is :" + k);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement