Goodiny777

7Boom_to_file

Mar 5th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package fileWork;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) throws IOException {
  9.         int userNum= 100;
  10.         String fileName = "boom.txt"; //for test
  11.         sevenBoom(fileName, userNum);
  12.  
  13.     }
  14.  
  15.     // file path C:\res\boomb.txt
  16.     public static void sevenBoom(String fileName, int userNum) throws IOException {
  17.         //creating new file and its PrintWriter to write in
  18.         File f = new File("C:\\res\\"+fileName);
  19.         f.createNewFile();
  20.         PrintWriter pw = new PrintWriter(f);
  21.         //go throught all numbers untill user's number
  22.         for (int i = 1; i<userNum;i+=1) {
  23.             //using help function which check if a number is 7Boom
  24.             if (containSeven(i)) {
  25.                 pw.print("7Boom!");
  26.                 pw.println();
  27.             } else {
  28.                 pw.print(i + " ");
  29.             }
  30.         }
  31.         pw.close();
  32.     }
  33.  
  34.     public static boolean containSeven(int num) {
  35.         int temp;
  36.         //true for numbers which could be divided by 7
  37.         if (num%7==0&&num%10!=0){
  38.             return true;
  39.         }
  40.         //thue for numbers which contains digit 7
  41.         while (num != 0) {
  42.             temp = num % 10;
  43.             if (temp%7==0&&temp%10!=0){
  44.                 return true;
  45.             }
  46.             num /= 10;
  47.         }
  48.         // for all others false
  49.         return false;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment