Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Basic Java* I just need to add a few methods to this code. I have attached the

ID: 3687620 • Letter: #

Question

*Basic Java* I just need to add a few methods to this code. I have attached the prompt and requirements (They are basically almost finished.) Thank you!

Mary. txt file --

1 Mary had a little lamb,

2 His fleece was white as snow,

3 And everywhere that Mary went,

4 The lamb was sure to go.

5

6 He followed her to school one day,

7 Which was against the rule,

8 It made the children laugh and play

9 To see a lamb at school.

10

11 And so the teacher turned it out,

12 But still it lingered near,

13 And waited patiently about,

14 Till Mary did appear.

15

16 "Why does the lamb love Mary so?"

17 The eager children cry.

18 "Why, Mary loves the lamb, you know."

19 The teacher did reply

20

22 And so the teacher turned him out

23 Turned him out, turned him out

24 And so the teacher turned him out

25 But still he lingered near

26 And waited patiently

27 Patiently, patiently

28 And wai-aited patiently

29 Til Mary did appear

CODE--

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.*;

public class HomeworkBuffer {

   public static void main(String[] args) throws IOException {   

   try

   {

   boolean tryAgain=true;

   while(tryAgain)

   {

   System.out.println("Enter File Location");

   Scanner scan=new Scanner(System.in);

   String File=scan.nextLine();

   BufferedReader reader=new BufferedReader(new FileReader(File));

   System.out.println("Enter Word You Want to Search");

   String pattern=scan.nextLine();

   System.out.println("You Have entered: "+ pattern);

   String currentLine;

   int count=0;

   int length =pattern.length();

   while((currentLine=reader.readLine())!=null)

   {

   int index=currentLine.indexOf(pattern);

   if(currentLine.contains(pattern))

   {

   System.out.println(currentLine);

   while( index !=-1)

   {

   count++;

   currentLine=currentLine.substring(index+length);

   index=currentLine.indexOf(pattern);

   }

   }

   }

   System.out.print("Total Occurence Of the Pattern: " + count);

   System.out.println(" Press 1 to try Again");

   int input=Integer.parseInt(scan.nextLine());

   if(input==1)

   {

   tryAgain=true;

   }

   else

   {

   tryAgain=false;

   }

   }

   }

   catch(Exception e)

   {

   }

   }

}

Explanation / Answer

Hi, Please find below updated code. Added 3 methods as per the requirement.

HomeworkBuffer.java


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class HomeworkBuffer {
public static void main(String[] args) throws IOException {
try
{
   File file = null;
boolean tryAgain=true;
BufferedReader reader = null;
while(tryAgain)
{
Scanner scan=new Scanner(System.in);
String File= getInputFileName(scan);
file = new File(File);
if(isFileExist(file)){
reader =new BufferedReader(new FileReader(File));
System.out.println("Enter Word You Want to Search");
String pattern=scan.next();
System.out.println("You Have entered: "+ pattern);
checkPatterns(reader, pattern);
System.out.println("Press 1 to try Again");
int input=Integer.parseInt(scan.next());
if(input==1)
{
tryAgain=true;
}
else
{
tryAgain=false;
}
}
else{
   System.out.println("File does not exist.");
}
}
if(reader !=null ) reader.close();

}
catch(Exception e)
{
   e.printStackTrace();
}
}
public static String getInputFileName(Scanner scan){
   System.out.println("Enter File Location");
   String fileName = scan.next();
   return fileName;
}
public static boolean isFileExist(File file){
   if(file.exists())
       return true;
   else
       return false;
}
public static void checkPatterns(BufferedReader reader, String pattern) throws IOException{
   String currentLine;
   int count=0;
   int length =pattern.length();
   while((currentLine=reader.readLine())!=null)
   {
   int index=currentLine.indexOf(pattern);
   if(currentLine.contains(pattern))
   {
   System.out.println(currentLine);
   while( index !=-1)
   {
   count++;
   currentLine=currentLine.substring(index+length);
   index=currentLine.indexOf(pattern);
   }
   }
   }
   System.out.println("Total Occurence Of the Pattern: " + count);
     
}
}

Output:

Enter File Location
D:\Mary.txt
Enter Word You Want to Search
lamb
You Have entered: lamb
1 Mary had a little lamb,
4 The lamb was sure to go.
9 To see a lamb at school.
16 "Why does the lamb love Mary so?"
18 "Why, Mary loves the lamb, you know."
Total Occurence Of the Pattern: 5
Press 1 to try Again
1
Enter File Location
D:\Mary.txt
Enter Word You Want to Search
teacher
You Have entered: teacher
11 And so the teacher turned it out,
19 The teacher did reply
22 And so the teacher turned him out
24 And so the teacher turned him out
Total Occurence Of the Pattern: 4
Press 1 to try Again
2