Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Tester;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Boom7Tester {
- public static void main(String[] args){
- String fileName = "SevenBoom.boom";
- int num = 39;
- try {
- writeFile(fileName, num);
- System.out.println(numOfBooms(fileName));
- }catch(FileNotFoundException fnfe) {
- System.out.println(fnfe);
- }
- }
- public static void writeFile(String fName, int n) throws FileNotFoundException {
- File file = new File(fName);
- PrintWriter pw = new PrintWriter(file);
- for(int i = 1; i <= n; i++) {
- if (i % 7 == 0 || contains7Digit(i)) {
- pw.println("boom");
- } else {
- pw.println(i);
- }
- }
- pw.close();
- }
- public static boolean contains7Digit(int number) {
- while(number/10 > 0) {
- if(number % 10 == 7) {
- return true;
- }
- number /= 10;
- }
- return false;
- }
- public static int numOfBooms(String fileName) throws FileNotFoundException{
- File f = new File(fileName);
- Scanner sc = new Scanner(f);
- int booms = 0;
- while(sc.hasNext()) {
- if(sc.nextLine().equals("boom")) {
- booms += 1;
- }
- }
- return booms;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement