Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- JFileChooser chooser = new JFileChooser();
- chooser.setDialogTitle("Vyberte soubor ke zpracování");
- chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
- chooser.addChoosableFileFilter(new FileNameExtensionFilter("Data v csv (*.csv)", "csv"));
- int result = chooser.showOpenDialog(this);
- if(result == JFileChooser.APPROVE_OPTION) {
- File file = chooser.getSelectedFile();
- if(file == null || !file.exists()){
- JOptionPane.showMessageDialog(this, "Chyba, soubor neexistuje!", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- if(!file.canRead()){
- JOptionPane.showMessageDialog(this, "Chyba, nemáš oprávnění k souboru!", "Chyba!", JOptionPane.ERROR_MESSAGE);
- return;
- }
- int count = 0;
- int max = Integer.MIN_VALUE;
- int lastTests = -1;
- try (BufferedReader br = new BufferedReader(new FileReader(file))){
- String line;
- boolean first = true;
- while ((line = br.readLine()) != null){
- if(first){
- first = false;
- continue;
- }
- String[] data = line.split(",");
- if(data.length != 3){
- continue;
- }
- //String dateRaw = data[0]; toto nepotřebujeme
- if(isNumber(data[1]) && isNumber(data[2])){
- int testsDay = getNumber(data[1]);
- int testsSum = getNumber(data[2]);
- /*
- Zde moc nerozumím v zadání "celkový počet položek - struktur odpovídající jednomu dni"
- Tak volím cestu "položky které mají správný syntax struktury
- */
- count++;
- lastTests = testsSum;
- if(testsDay > max){
- max = testsDay;
- }
- }
- }
- } catch (Exception ignored){
- }
- double avg = (double) lastTests / (double) count;
- String out = "";
- out += "<p>Počet položek: <b>" + count + "</b></p>";
- out += "<p>Poslední počet testovaných: <b>" + lastTests + "</b></p>";
- out += "<p>Průměr testů: <b>" + String.format("%.2f", avg) + "</b></p>";
- out += "<p>Nejvíce otestovaných za jeden dne: <b>" + max + "</b></p>";
- output.setText(out);
- }else{
- JOptionPane.showMessageDialog(this, "Soubor nebyl vybrán...", "Chyba!", JOptionPane.ERROR_MESSAGE);
- }
- }
- public int getNumber(String s){
- try{
- return Integer.parseInt(s);
- }catch(Exception ignored){
- return 0;
- }
- }
- public boolean isNumber(String s){
- try{
- int i = Integer.parseInt(s);
- return true;
- }catch(Exception ignored){
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement