home / study / engineering / computer science / computer science questions and a
ID: 3738540 • Letter: H
Question
home / study / engineering / computer science / computer science questions and answers / In Great Need For Help Of C++programing :) Thanks Vey Much!! SIXPENCE.txt: Sing A Song Of Sixpence, ...
Your question has been posted.
We'll notify you when a Chegg Expert has answered. Post another question.
Next time just snap a photo of your problem. No typing, no scanning, no explanation required.
Get Chegg Study App
Question: In great need for help of c++programing :) thanks vey much!! SIXPENCE.txt: Sing a song ...
Edit question
In great need for help of c++programing :) thanks vey much!!
SIXPENCE.txt:
Sing a song of sixpence,
A pocket full of rye;
Four and twenty blackbirds
Baked in a pie.
When the pie was opened,
They all began to sing.
Now, wasn't that a dainty dish to set before the King?
The King was in the countinghouse,
Counting out his money;
The Queen was in the parlor
Eating bread and honey.
The maid was in the garden,
Hanging out the clothes.
Along there came a big black bird
And snipped off her nose!
Explanation / Answer
Hello There,
PFB requested code in java along with the output of test run
Prog.java
---------------------------------------
package dummy;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Tester {
public static void main(String[] args) {
List<String> wordList = new ArrayList<String>();
System.out.println("Please enter a command:");
Scanner scanner = new Scanner(System.in);
String command = scanner.nextLine();
while(command != null) {
if(command.startsWith("load")) {
String[] commandDetail = command.split(" ");
if(commandDetail.length == 2) {
String filename = commandDetail[1];
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String str;
while((str = br.readLine()) != null) {
for (String word : str.split(" ")) {
for(int i=0; i<word.length(); i++) {
char ch = word.charAt(i);
if(!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == ''')) {
word = word.substring(0, i) + " " + word.substring(i+1);
}
}
for (String finalWord : word.split(" ")) {
wordList.add(finalWord);
}
}
}
} catch (Exception e) {
System.out.println("ERROR: Invalid command");
}
}else {
System.out.println("ERROR: Invalid command");
}
}else if(command.startsWith("locate")) {
String[] commandDetail = command.split(" ");
if(commandDetail.length == 3) {
String word = commandDetail[1];
int count = Integer.parseInt(commandDetail[2]);
for (int i=0; i<wordList.size(); i++) {
String wordInList = wordList.get(i);
if(wordInList.equals(word)) {
count--;
if(count == 0) {
System.out.println(i+1);
break;
}
}
}
if(count > 0) {
System.out.println("No matchinig entry");
}
}else {
System.out.println("ERROR: Invalid command");
}
}else if(command.equals("new")) {
wordList = new ArrayList<String>();
}else if(command.equals("end")) {
System.exit(0);
}else {
System.out.println("ERROR: Invalid command");
}
command = scanner.nextLine();
}
scanner.close();
}
}
----------------------------------------------
Test run
---------------------------------------------
Please enter a command:
load sixpence.txt
locate song 1
3
locate pie 1
18
locate pie 2
21
locate pie 3
No matchinig entry
locate prince
ERROR: Invalid command
locate prince 1
No matchinig entry
new
locate song 1
No matchinig entry
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.