From ea20573e9702936995f58de78ee7e644de395c9d Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 29 Aug 2023 08:47:24 -0700 Subject: [PATCH 1/2] Fixed bug & wrote tester made 'fileName' a parameter in both methods instead of it being a preset variable. wrote a tester --- Milo.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Milo.java b/Milo.java index 687938e..0cb7fba 100644 --- a/Milo.java +++ b/Milo.java @@ -2,18 +2,22 @@ import java.io.*; public class Milo { - public void writeToFile(String s) throws IOException { - String fileName = "milo.txt"; + public static void writeToFile(String s, String fileName) throws IOException { + // String fileName = "milo.txt"; PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); pw.print(s); pw.close(); } - public String readFromFile() throws IOException { - String fileName = "milobear.txt"; + public static String readFromFile(String fileName) throws IOException { + // String fileName = "milobear.txt"; BufferedReader br = new BufferedReader(new FileReader(fileName)); String s = br.readLine(); br.close(); return s; } + public static void main(String[] args) throws IOException { + // writeToFile("milo","milo.txt"); + System.out.println(readFromFile ("milo.txt")); + } } From 0b3093d3671feb50d682f1f84424a3a62ec2d32c Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 29 Aug 2023 08:56:02 -0700 Subject: [PATCH 2/2] wrote countCharacters and updated tester --- Milo.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Milo.java b/Milo.java index 0cb7fba..b5214e0 100644 --- a/Milo.java +++ b/Milo.java @@ -16,8 +16,20 @@ public static String readFromFile(String fileName) throws IOException { br.close(); return s; } + public static int countCharacters (String fileName) throws IOException + { + int count = 0; + BufferedReader br = new BufferedReader(new FileReader(fileName)); + while (br.ready()) + { + br.read(); + count++; + } + return count; + } public static void main(String[] args) throws IOException { // writeToFile("milo","milo.txt"); System.out.println(readFromFile ("milo.txt")); + System.out.println(countCharacters("milo.txt")); } }