Advertisement
diablo878

Untitled

Jan 13th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.JButton;
  3. import javax.swing.JFileChooser;
  4. import javax.swing.JTextField;
  5. import java.io.File;
  6. import java.util.Scanner;
  7. import java.awt.event.*;
  8.  
  9. import javax.swing.*;
  10.  
  11. public class ZadanieNowe extends JFrame implements ActionListener {
  12.     public JButton browser;
  13.     public JTextField enterTextField;
  14.     public JTextField text;
  15.     public static File chosenFile;
  16.  
  17.     public ZadanieNowe() {
  18.  
  19.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20.         setSize(400, 200);
  21.         setTitle("StatystykaPliku");
  22.  
  23.         text = new JTextField();
  24.         browser = new JButton("Browse File");
  25.  
  26.         add(browser, BorderLayout.NORTH);
  27.         add(text, BorderLayout.CENTER);
  28.         setVisible(true);
  29.  
  30.         text.addActionListener(this);
  31.         browser.addActionListener(this);
  32.  
  33.     }
  34.  
  35.    
  36.  
  37.     public void actionPerformed(ActionEvent event) {
  38.  
  39.         JFileChooser fc = new JFileChooser();
  40.         if (event.getSource() == browser) {
  41.  
  42.             if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  43.                 chosenFile = fc.getSelectedFile();
  44.                 JOptionPane.showMessageDialog(null, "Chosen File is: "
  45.                         + chosenFile.getAbsolutePath());
  46.             }
  47.  
  48.         }
  49.  
  50.     }
  51.  
  52.     public static void main(String[] args) {
  53.  
  54.         new ZadanieNowe();
  55.        
  56.  
  57.         int i = 0;
  58.         int j = 0;
  59.         int k = 0;
  60.         try {
  61.             Scanner in = new Scanner(chosenFile);
  62.             while (in.hasNextLine()) {
  63.                 String a = in.nextLine();
  64.                 k++;
  65.                 char chars[] = a.toCharArray();
  66.                 i += chars.length;
  67.             }
  68.             in.close();
  69.             Scanner in2 = new Scanner(chosenFile);
  70.             while (in2.hasNext()) {
  71.  
  72.                 in2.next();
  73.  
  74.                 j++;
  75.             }
  76.             in2.close();
  77.  
  78.             System.out.println("the number of chars is :" + i);
  79.             System.out.println("the number of words is :" + j);
  80.             System.out.println("the number of lines is :" + k);
  81.  
  82.         } catch (Exception e) {
  83.             e.printStackTrace();
  84.  
  85.         }
  86.  
  87.     }
  88.    
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement