Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1.         JFileChooser dialog = new JFileChooser();
  2.         dialog.setDialogTitle("Vyberte CSV soubor ke zpracování");
  3.         dialog.setFileFilter(new FileNameExtensionFilter("CVS soubory", "csv"));
  4.         if(dialog.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
  5.             File file = dialog.getSelectedFile();
  6.             if(file.exists() && file.canRead()){
  7.                 try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  8.                     String line;
  9.                     double count = 0;
  10.                     int pocet = 0;
  11.                     while((line = br.readLine()) != null){
  12.                         line = line.trim();
  13.                         if(line.length() > 0){
  14.                             String[] values = line.split(";");
  15.                             for(String value : values){
  16.                                 double d;
  17.                                 try{
  18.                                     d = Double.parseDouble(value.replace(",", "."));
  19.                                     pocet++;
  20.                                 }catch(NumberFormatException ex){
  21.                                     d = 0;
  22.                                 }
  23.                                 count += d;
  24.                             }
  25.                         }
  26.                     }
  27.                     JOptionPane.showMessageDialog(this, "Součet hodnot je " + count + "\nPrůměr " + (count/pocet));
  28.                 } catch (IOException ex) {
  29.                     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  30.                 }
  31.             }
  32.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement