Advertisement
eranseg

7Boom

Sep 11th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package Tester;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.PrintWriter;
  6. import java.util.Scanner;
  7.  
  8. public class Boom7Tester {
  9.     public static void main(String[] args){
  10.         String fileName = "SevenBoom.boom";
  11.         int num = 39;
  12.         try {
  13.             writeFile(fileName, num);
  14.             System.out.println(numOfBooms(fileName));
  15.         }catch(FileNotFoundException fnfe) {
  16.             System.out.println(fnfe);
  17.         }
  18.     }
  19.  
  20.     public static void writeFile(String fName, int n) throws FileNotFoundException {
  21.         File file = new File(fName);
  22.         PrintWriter pw = new PrintWriter(file);
  23.         for(int i = 1; i <= n; i++) {
  24.             if (i % 7 == 0 || contains7Digit(i)) {
  25.                 pw.println("boom");
  26.             } else {
  27.                 pw.println(i);
  28.             }
  29.         }
  30.         pw.close();
  31.     }
  32.  
  33.     public static boolean contains7Digit(int number) {
  34.         while(number/10 > 0) {
  35.             if(number % 10 == 7) {
  36.                 return true;
  37.             }
  38.             number /= 10;
  39.         }
  40.         return false;
  41.     }
  42.  
  43.     public static int numOfBooms(String fileName) throws FileNotFoundException{
  44.         File f = new File(fileName);
  45.         Scanner sc = new Scanner(f);
  46.         int booms = 0;
  47.         while(sc.hasNext()) {
  48.             if(sc.nextLine().equals("boom")) {
  49.  
  50.                 booms += 1;
  51.             }
  52.         }
  53.         return booms;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement