Question

JAVA Primitive Editor The primary goal of the assignment is to develop a Java based primitive...

JAVA Primitive Editor
The primary goal of the assignment is to develop a Java based primitive editor.  We all know what an editor of a text file is.  Notepad, Wordpad, TextWrangler, Pages, and Word are all text editors, where you can type text, correct the text in various places by moving the cursor to the right place and making changes.  The biggest advantage with these editors is that you can see the text and visually see the edits you are making.  Back in the day, editing was not always visual but command driven instead.  

One such is a form of editor is a Basic File Editor which will do the following types of edits using suitable methods.  Using the methods you won’t have any visual way of editing the files.  Instead, editing is done via a command which, in turn, is executed by the corresponding method.  

1. boolean Find (String x) // Looks for a word "x" in the file and returns true if found or false otherwise.
2. boolean FindReplace (String x, String y) // looks for the first occurrence of word "x" in the file and replaces it with word "y" if found returning true, false otherwise.
3. boolean FindInsert (String x, String y)  // looks for the first occurrence of word "x" in the file and then insert "y" right after "x", if x is found, returning true, false otherwise.  
4. boolean Delete (String x) // looks for the first occurrence of word "x" in the file and deletes it from the file, returning true if x is found, returning false otherwise.
5. String spellCheck ()  // finds the first occurrence of spelling error and returns the misspelled word. If no word is misspelled returns "Spell Check Passed".
6. void spellCheckAll() // find all misspelled words and output them to the screen.  
7. void save() // saves file with the changes made.
8. void print() // saves file with the changes and outputs the contents of the file to the screen.
9. void quit() should save() the file and exit.
10. boolean FindReplaceAll (String x, String y) // looks for all occurrences of word "x" in the file and replace each with word "y" if found returning true, false otherwise.
-------------
You have learned all the pieces needed to develop the editor. Here are some ideas you want to think about:
1.      How do you represent the text file (to be edited) in memory? In other words, what data structure do you want to use that will store the file?
2.      Once you store the file in a data structure, you know you can implement the methods that will act on the data structure.  
3.      Please ensure that the file X to be manipulated is read into memory first and after a few edits may be saved to a file F. Subsequent edits should use file F rather than file X.
4.      How do you actually save all the changes? What does it mean to save all the changes made to the file?
5.      When you read a formatted text file into memory, make some edits on it, and then save it by writing it back to a different file (name), you may lose the format.  And that is ok for this assignment
6.      Do a conceptual design first. ALWAYS. Don't jump into coding. What is conceptual design? You should be able to say or write in plain English -- ok this is how I am going to split the problem into various smaller tasks and this is how I am going to do each task.  You don’t need to submit it necessarily, but I will be happy to review it if you do.  
7.      Present a menu of choices of all the 10 methods for the user to choose from and number them 1,2,3, ... , 9, 10. If the user types number 2, it means that the user wants to FindReplace(...). For this one, further prompt the user to enter two strings needed for this method to work. And so on. Additionally, you may have a choice for the user to terminate the session, for example, typing "exit" or "done" will cause everything to be saved and terminating the execution of the editor.
8.      I want you to think about this and come to class prepared to discuss it. You must use the  text files attached to test your code with.
9.      For spellcheck, find, findreplace, findinsert,findreplaceall, etc, use the idea in 11 below.  
10.     You can either output numbers, conjugations or declensions of words, proper nouns, dates etc as spelling errors or ignore them. 
11.     To get rid of most punctuations, leading and trailing blank spaces, and also for comparing with the dictionary, use 

String s,t ; 
s = t.replaceAll("[\\[\\]_:\"'`?;\\”0-9—;“()-/.,*! ]", "").trim().toLowerCase();

12.  You will use the EnglishWordList.txt as the dictionary to compare words in the text file for editing.  Both of them wil have to be read into memory.  
13.  You can read each line of the text file into memory and then split the line into words using the delimiter blank space as below.  
String[] wordsInLine = line.split(" ");  
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.*;
class PrimitiveEditor{

PrimitiveEditor() throws Exception{
File file = new File("TestFile.txt");
BufferedReader br = new BufferedReader(new FileReader(file));

FileWriter fw=new FileWriter("TempFile.txt");

String s;

while ((s = br.readLine()) != null) {
fw.write(s+"\r");
}
fw.close();
}
boolean Find(String x) throws Exception{
String s;
String[] words=null;
File file2 = new File("TempFile.txt");
BufferedReader br2 = new BufferedReader(new FileReader(file2));
while ((s = br2.readLine()) != null) {
words=s.split(" "); //Split the word using space
for (String word : words)
{
if (word.equalsIgnoreCase(x)) //Search for the given word
return true;
}
}return false;
}
boolean FindReplace(String x, String y) throws Exception{
String line = "", oldtext = "";
File file2 = new File("TempFile.txt");
BufferedReader br2 = new BufferedReader(new FileReader(file2));
while((line = br2.readLine()) != null) {
oldtext += line + "\r\n";
}
if(oldtext.indexOf(x)<0)
   return false;
   else
   {
   String newtext = oldtext.replaceFirst(x,y);
FileWriter writer = new FileWriter("TempFile.txt");
writer.write(newtext);writer.close();
}
return true;
}
void save() throws Exception{
InputStream in = new FileInputStream(new File("TempFile.txt"));
OutputStream out = new FileOutputStream(new File("TestFile.txt"));
byte[] buf = new byte[1024];
int len;
  
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();

System.out.println("**** Changes saved ****");
}
void quit() throws Exception{
save();
File file = new File("TempFile.txt");
file.delete();

System.exit(0);
}

void print() throws Exception{
File file2 = new File("TempFile.txt");
BufferedReader br2 = new BufferedReader(new FileReader(file2));
String s="";
while ((s = br2.readLine()) != null) {
System.out.println(s);
}
}

/* 4 methods not implemented

boolean FindInsert(String x, String y) {}
boolean Delete(String x) {}
String spellCheck() {}
void spellCheckAll() {}

*/
boolean FindReplaceAll(String x, String y) throws Exception{
String line = "", oldtext = "";
File file2 = new File("TempFile.txt");
BufferedReader br2 = new BufferedReader(new FileReader(file2));
while((line = br2.readLine()) != null) {
oldtext += line + "\r\n";
}
if(oldtext.indexOf(x)<0)
   return false;
   else
   {
   String newtext = oldtext.replace(x,y);
FileWriter writer = new FileWriter("TempFile.txt");
writer.write(newtext);writer.close();
}
return true;
}

public static void main(String ar[])throws Exception{
PrimitiveEditor pe=new PrimitiveEditor();
while(true){
System.out.println("Enter your choice");
System.out.println("1. Find");
System.out.println("2. FindReplace");
System.out.println("3. FindReplaceAll");
System.out.println("4. FindInsert");
System.out.println("5. Delete");
System.out.println("6. spellCheck");
System.out.println("7. spellCheckAll");
System.out.println("8. save");
System.out.println("9. print");
System.out.println("10. quit");
Scanner sc=new Scanner(System.in);
int ch=sc.nextInt();
sc.nextLine();
String x1,x2;

switch(ch){
case 1:
   System.out.println("Enter the word to be searched");
   x1=sc.nextLine()+" ";
if(pe.Find(x1))
       System.out.println("The word is available in file");
       else
       System.out.println("oops!! The word is NOT available in file");

break;
case 2:
System.out.println("Enter the word to be replaced");
   x1=sc.nextLine()+" ";
   System.out.println("Enter the new word");
   x2=sc.nextLine()+" ";
if(pe.FindReplace(x1,x2))
       System.out.println("The word is replaced in file");
       else
       System.out.println("oops!! The word is NOT available in file");

break;
case 3:
System.out.println("Enter the word to be replaced");
   x1=sc.nextLine()+" ";
   System.out.println("Enter the new word");
   x2=sc.nextLine()+" ";
if(pe.FindReplaceAll(x1,x2))
       System.out.println("The word is replaced in file");
       else
       System.out.println("oops!! The word is NOT available in file");
break;
case 4:
  
break;
case 5:
  
break;
case 6:
  
break;
case 7:
  
break;
case 8:
pe.save();
break;
case 9:
pe.print();
break;
case 10:
pe.quit();
break;

}
}
}
}

Add a comment
Know the answer?
Add Answer to:
JAVA Primitive Editor The primary goal of the assignment is to develop a Java based primitive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Primitive Editor (Please help, I am stuck on this assignment which is worth a lot...

    JAVA Primitive Editor (Please help, I am stuck on this assignment which is worth a lot of points. Make sure that the program works because I had someone answer this incorrectly!) The primary goal of the assignment is to develop a Java based primitive editor. We all know what an editor of a text file is. Notepad, Wordpad, TextWrangler, Pages, and Word are all text editors, where you can type text, correct the text in various places by moving the...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly...

    Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly typed and statically typed Everything has a data type & data types must be declared Case sensitive Object oriented System.out.print() vs. System.out.println() How to use the Scanner class to obtain user input Data typesWhat are they? Know the basic types like: int, double, boolean, String, etc. Variables What is a variable? Declarations Initialization Assignment Constants Reserved words like: What is a reserved word? Examples:...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program...

    Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the input file to be spell checked. The program will read in the words for the dictionary, then will read the input file and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is, add...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • *JAVA File Input/Output Homework* (4 parts) Part 1 1) Open a TEXT editor program 2) Type...

    *JAVA File Input/Output Homework* (4 parts) Part 1 1) Open a TEXT editor program 2) Type in 50 words, one word per line, all words of the SAME catagory, max chars per word is 8. Example: 50 words of some names in the periodic table, or Flowers, or cars or.... 3) Save the file as myWordFile.txt Part 2 Write a program the reads the myWordFile.txt 1) Create an array of 'strings' called myWordArray 2) Write code that opens the file...

  • Java This assignment will give you practice with line based file processing and scanner methods. Modify...

    Java This assignment will give you practice with line based file processing and scanner methods. Modify the Hours program we did in class. You are going to write a program that allows the user to search for a person by ID. Your program is required to exactly reproduce the format and behavior of the log of execution as follows: Enter an ID: 456 Brad worked 36.8 hours (7.36 hours/day) Do you want to search again? y Enter an ID: 293...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT