Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JFileChooser dialog = new JFileChooser();
- dialog.setDialogTitle("Vyberte CSV soubor ke zpracování");
- dialog.setFileFilter(new FileNameExtensionFilter("CVS soubory", "csv"));
- if(dialog.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
- File file = dialog.getSelectedFile();
- if(file.exists() && file.canRead()){
- try (BufferedReader br = new BufferedReader(new FileReader(file))) {
- String line;
- double count = 0;
- int pocet = 0;
- while((line = br.readLine()) != null){
- line = line.trim();
- if(line.length() > 0){
- String[] values = line.split(";");
- for(String value : values){
- double d;
- try{
- d = Double.parseDouble(value.replace(",", "."));
- pocet++;
- }catch(NumberFormatException ex){
- d = 0;
- }
- count += d;
- }
- }
- }
- JOptionPane.showMessageDialog(this, "Součet hodnot je " + count + "\nPrůměr " + (count/pocet));
- } catch (IOException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement