Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Lesson016_11_09_19_Task1 {
- private static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) throws IOException, FileNotFoundException {
- String filename = "task1.txt";
- System.out.print("Please enter a number: ");
- int num = sc.nextInt();
- sevenBoom(filename, num);
- System.out.printf("The world boom count is: " + sevenBoomCount(filename) + " times");
- notepad(filename); //for testings
- }
- // function for task1
- public static void sevenBoom(String filename, int num) throws FileNotFoundException {
- File f = new File(filename);
- PrintWriter pw = new PrintWriter(f);
- pw.print("The 7-boom game:\n");
- String str = "";
- for (int i = 1; i <= num; i += 1) {
- str += (i % 7 == 0 || i % 10 == 7 || (i / 10) % 10 == 7) ? ("Boom!\n") : (i + "\n");
- }
- System.out.print(str);
- pw.print(str);
- pw.close();
- }
- public static int sevenBoomCount(String filename) throws FileNotFoundException {
- File file = new File(filename);
- int count = 0;
- Scanner sc1 = new Scanner(file);
- while (sc1.hasNextLine()) {
- String nextLine = sc1.nextLine();
- if (nextLine.equals("Boom!"))
- count++;
- //sc.nextLine();
- }
- sc1.close();
- return count;
- }
- // function for auto-open on Notepad++
- public static void notepad(String filename) throws IOException {
- Runtime runtime = Runtime.getRuntime();
- Process process = runtime.exec("C:\\Program Files\\Notepad++\\notepad++.exe " + filename);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement