**Basic Java** I just need to build up on this previous code. I have to read in
ID: 3687599 • Letter: #
Question
**Basic Java** I just need to build up on this previous code. I have to read in the file Mary.txt. which i will attach here, and ask the user for input to search something in the text file. I have attached the requirements and the rubric. The code is also attached. Thank you!
Mary.txt --
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.FileReader;
import java.io.IOException;
import java.util.*;
public class HomeworkBuffer {
public static void main(String[] args) {
BufferedReader reader = null;
ArrayList<String> myFileLines = new ArrayList<String>();
try {
String sCurrentLine;
reader = new BufferedReader(new FileReader("/Users/markscomp/Desktop/mary.txt")); //This will be different for windows users
while (( sCurrentLine = reader.readLine()) !=null) {
System.out.println(sCurrentLine);
myFileLines.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
System.out.print(e.getMessage());
} finally {
try {
if (reader != null)reader.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
try
{
bool 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)
{
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.