Advertisement
RexyBadDog

Lesson016_11_09_19_Task1

Sep 11th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Scanner;
  6.  
  7. public class Lesson016_11_09_19_Task1 {
  8.     private static Scanner sc = new Scanner(System.in);
  9.  
  10.     public static void main(String[] args) throws IOException, FileNotFoundException {
  11.         String filename = "task1.txt";
  12.         System.out.print("Please enter a number: ");
  13.         int num = sc.nextInt();
  14.         sevenBoom(filename, num);
  15.         System.out.printf("The world boom count is: " + sevenBoomCount(filename) + " times");
  16.         notepad(filename);  //for testings
  17.     }
  18.  
  19.     // function for task1
  20.     public static void sevenBoom(String filename, int num) throws FileNotFoundException {
  21.         File f = new File(filename);
  22.         PrintWriter pw = new PrintWriter(f);
  23.         pw.print("The 7-boom game:\n");
  24.         String str = "";
  25.         for (int i = 1; i <= num; i += 1) {
  26.             str += (i % 7 == 0 || i % 10 == 7 || (i / 10) % 10 == 7) ? ("Boom!\n") : (i + "\n");
  27.         }
  28.         System.out.print(str);
  29.         pw.print(str);
  30.         pw.close();
  31.     }
  32.  
  33.     public static int sevenBoomCount(String filename) throws FileNotFoundException {
  34.         File file = new File(filename);
  35.         int count = 0;
  36.         Scanner sc1 = new Scanner(file);
  37.         while (sc1.hasNextLine()) {
  38.             String nextLine = sc1.nextLine();
  39.             if (nextLine.equals("Boom!"))
  40.                 count++;
  41.             //sc.nextLine();
  42.  
  43.         }
  44.         sc1.close();
  45.         return count;
  46.     }
  47.  
  48.     // function for auto-open on Notepad++
  49.     public static void notepad(String filename) throws IOException {
  50.         Runtime runtime = Runtime.getRuntime();
  51.         Process process = runtime.exec("C:\\Program Files\\Notepad++\\notepad++.exe " + filename);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement