Facebook
From Sylwia, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 214
  1. package egzamin;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.nio.file.Paths;
  10. import java.nio.file.StandardOpenOption;
  11. import java.util.Scanner;
  12.  
  13.  
  14.          
  15.         public class Egzamin{
  16.          
  17.  
  18.         public static void main(String[] args) {
  19.                         obliczNadwage("wagi.txt");
  20.                 }
  21.                
  22.                 public static void obliczNadwage(String plik){
  23.                  
  24.                    
  25.  
  26.                         File file = new File(plik);
  27.                         Scanner scan = null;
  28.                         try {
  29.                                 scan = new Scanner(file);
  30.                                 scan.useDelimiter("-|\\n");
  31.                                
  32.                                 while(scan.hasNext()) {
  33.                                         String imie = scan.next();
  34.                                         String plec = scan.next();
  35.                                         double waga = Double.parseDouble(scan.next());
  36.                                         double wzrost = Double.parseDouble(scan.next());
  37.          
  38.                                         double bmi = waga/(wzrost*wzrost);
  39.                                        
  40.                                         String fileName = null;
  41.                                         if(bmi<18.5) {
  42.                                                 fileName = new String("niedowaga");
  43.                                         } else if(bmi>25.0) {
  44.                                                 fileName = new String("nadwaga");
  45.                                         }
  46.                                                                                                            
  47.                                         if(fileName!=null) {
  48.                                            
  49.                                            
  50.                                                 Path path = Paths.get(fileName + ".bin");
  51.                                                 try {
  52.                                                         BufferedWriter bufferedWriter = Files.newBufferedWriter(path, StandardOpenOption.CREATE);
  53.                                                         bufferedWriter.write(imie);
  54.                                                         bufferedWriter.write(System.getProperty("line.separator"));
  55.                                                         bufferedWriter.write(String.valueOf(bmi));
  56.                                                         bufferedWriter.flush();
  57.                                                        
  58.                                                         System.out.println("flushed");
  59.                                                        
  60.                                                 } catch (IOException e) {
  61.                                                         e.printStackTrace();
  62.                                                 }
  63.                                         }
  64.                                 }
  65.                                
  66.                         } catch (FileNotFoundException e) {
  67.                                 // TODO Auto-generated catch block
  68.                                 e.printStackTrace();
  69.                         } finally {
  70.                                 scan.close();
  71.                         }
  72.                        
  73.                 }