Results 1 to 9 of 9
  1. #1
    xquizitpinay is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default line editor program help!!

    I'm having problems with my code, it's not complete but any help would be great....

    I've attached my code as a .txt file.

    But this is the original problem:
    The Program

    Your program will be a line editor. A line editor is an editor where all operations are performed by entering commands at the command line. Commands include displaying lines, inserting text, editing lines, cutting and pasting text, loading and saving files. For example, a session where the user enters three lines of text and saves them as a new file may appear as:

    Menu: m Delete line: dl
    Load file: l Delete range: dr
    Show all: sa Copy range: cr
    Show line: sl Paste lines: pl
    Show range: sr Write to file: w
    New line: nl Quit: q
    Edit line: el Write and quit: wq
    -> nl

    type line? (y/n): y
    1: this is the first line
    type line? (y/n): y
    2: and a second line

    type line? (y/n): y
    3: and this is the third line
    type line? (y/n): n

    -> w

    write to file: test.txt

    -> sa

    1: this is the first line
    2: and a second line
    3: and this is the third line

    -> q

    The file created is:

    this is the first line
    and a second line
    and this is the third line

    The editor will first display a menu after which it will prompt for commands with "-> ". The commands which can be entered can be seen in the menu appearing in the example.

    This web page was written with the line editor described here.
    The Commands

    If you run the program with no command line arguments it will begin with an empty document. If you give the name of an existing file as a command line argument it will open and load the contents of that file. You can exit the program with either the quit, q, or write and quit, wq, command.
    Displaying the menu

    The menu is displayed when the program starts. It can be displayed with the command m.
    Reading and writing files

    The load file command l will ask the user for a file name, open the file for reading, read the file contents into the editor and close the file. If there is already text in the editor it is discarded. The write file command w will write the contents of the editor to a file. If a file has previously been opened with the read file command that same file is used for writing. Otherwise, the program will prompt the user for a file name. For either the read or write file commands if the requested file cannot be opened an appropriate error message is displayed.
    Displaying text

    Text can be displayed with three commands: show all (sa), show range (sr), and show line (sl). All three commands display text with line numbers. Show all displays the entire document. Show range will ask the user a range of lines ("from" and "to") and will display the lines in that range. If the "to" line number is greater than the number of lines in the document lines up to the end of the document will be displayed. Show line will ask the user for a line number and display that line.
    Entering new lines of text

    New lines of text are entered using the new line command nl. Before entering a new line the program will ask type line? (y/n). If the user chooses yes the line will be accepted after the line number is displayed. If the document is empty the new line will be the first line (line 1) of the document. If the document is not empty the program will prompt the user to enter the line after which the new line will be placed. If the line number is out of range the program will not accept a new line. A new first line can be added by asking to add a line after (the non-existent) line 0. After the user has entered a line the program will continue asking for new lines until the user answers no. As the new lines are being entered they are added sequentially to the document. As the user begins to enter lines at the new line command the line after which the new line is being added is first displayed (unless the user is entering lines at the beginning of the document). Examples:

    -> sa

    1: this is the first line
    2: and a second line
    3: and this is the third line

    -> nl
    insert after line number: 1
    inserting after:
    this is the first line
    type line? (y/n): y
    2: one and a half
    type line? (y/n): y

    3: one and three quarters
    type line? (y/n): n

    -> sa

    1: this is the first line
    2: one and a half
    3: one and three quarters
    4: and a second line
    5: and this is the third line

    ->

    The buffers

    There are two buffers, the line buffer and the string buffer, used for moving blocks of text. The line buffer holds ranges of entire lines and the string buffer holds substrings of individual lines. Whenever a new range of lines is copied to the line buffer or a string is copied to the string buffer the previous contents of that buffer are deleted (but the other buffer is unaffected).
    Moving ranges of lines

    Blocks of lines can be moved using copy range, cr, and paste lines, pl. Copy range will ask the user for a range of line numbers ("from" and "to") and will copy the lines to the line buffer - the lines remain unchanged in the document. Paste lines will ask the user to enter the number of the line after which the lines from the line buffer will be pasted. The lines are then copied into the document - the line buffer is left unchanged. Ranges of lines are deleted with delete range, dr. Again, the user is asked for the range of line numbers to delete. The lines are then deleted (the line buffer is unchanged).
    Editing a line of text

    The edit line command, el will ask for the number of a line to edit then display the line preceded by a scale which allows the user to identify positions in the line by number (beginning with 0). Then the line menu is displayed and the program prompts for input.

    -> el

    line number: 3
    0 5 10 15 20
    |----+----|----+----|-
    one and three quarters

    Show line: s
    Copy to string buffer: c
    Cut: t
    Paste from string buffer: p
    Enter new substring: e
    Delete substring: d
    Quit line: q
    ->

    The scale is at least as long as the line displayed. Operations chosen from the line menu will then operate on this chosen line until quit line, q.

    Show line will display the line (again) with the scale. Quit returns to the main editor menu. The remaining commands edit the chosen line.

    Copy to string buffer and Paste from string buffer work like copy range and paste lines but they work with the string buffer rather than the line buffer. Also, Paste from string buffer will insert the substring so that it begins at the specified position. (Thus it does an "insert before" where paste lines does an "insert after.")

    -> c
    0 5 10 15 20
    |----+----|----+----|-
    one and three quarters
    from position: 4
    to position: 7
    copied:
    0 5 10 15 20
    |----+----|----+----|-
    one and three quarters
    ^^^^
    Show line: s
    Copy to string buffer: c
    Cut: t
    Paste from string buffer: p
    Enter new substring: e
    Delete substring: d
    Quit line: q
    -> p
    0 5 10 15 20
    |----+----|----+----|-
    one and three quarters
    insert at position: 14
    0 5 10 15 20 25
    |----+----|----+----|----+
    one and three and quarters

    Show line: s
    Copy to string buffer: c
    Cut: t
    Paste from string buffer: p
    Enter new substring: e
    Delete substring: d
    Quit line: q
    ->

    Delete substring, t, will prompt for a range of characters to delete, will display the string with the substring underlined, ask the user if the range is ok, and then delete the substring.

    -> el

    line number: 1
    0 5 10 15 20
    |----+----|----+----|-
    this is the first line

    Show line: s
    Copy to string buffer: c
    Cut: t
    Paste from string buffer: p
    Enter new substring: e
    Delete substring: d
    Quit line: q
    -> d

    0 5 10 15 20
    |----+----|----+----|-
    this is the first line
    from position: 12
    to position: 18
    delete:
    0 5 10 15 20
    |----+----|----+----|-

    this is the first line
    ^^^^^^^
    y/n: y
    0 5 10 15
    |----+----|----+
    this is the ine

    Cut, t, combines copy to string buffer and delete substring. It copies the substring to the string buffer and removes it from the line.

    Enter new substring, e, will allow the user to type new text into an existing line.

    line number: 1
    0 5 10 15 20
    |----+----|----+----|-
    this is the first line

    Show line: s
    Copy to string buffer: c
    Cut: t
    Paste from string buffer: p
    Enter new substring: e
    Delete substring: d
    Quit line: q
    -> e

    0 5 10 15 20
    |----+----|----+----|-
    this is the first line
    insert at position: 12
    text: very very
    0 5 10 15 20 25 30
    |----+----|----+----|----+----|-
    this is the very very first line

    Internals

    You will define (among others) two classes: a Line class and a Document class. The Line class will hold the text of a line from the document in a field of type String and provide basic operations on a line. Some of these operations are insert a string into the line at a specified position, delete a substring, copy a substring, etc. The Line class will also have a field with the number of characters stored in the line. The Document class will hold an entire document (each line in one String) and provide basic operations such as inserting lines, deleting, copying, and pasting ranges of lines. It will also have a field with the number of lines currently in the document. Elsewhere in your program you will provide code for the operations (chosen by the user from the menu) which will then call the member functions of the Line and Document classes.

    The document will be a doubly linked list of lines (Strings) with a header node. I.e., if there are 100 lines in the document there will be 101 nodes in the linked list - 100 holding text and one header node. This will make insertions and deletions much easier by eliminating special boundary conditions. Line numbers will not be stored with lines. Instead, every time a line number is needed (e.g. for display in show range and show all, finding positions for new line, etc.) it will be found by traversing the list and counting.

    All strings will be Java Strings. Lines of text can be manipulated with concatenation and the substring methods:

    String substring(int beginIndex) // Returns a new string that is a
    // substring of this string.
    String substring(int beginIndex, int endIndex) // Returns a new string
    // that is a substring of
    // this string.

    To accept a new line of user input you must use nextLine since reading with next stops accepting input at the first white space and text lines may contain white space. You must use nextLine for all keyboard input: nextLine does not mix well with next, nextInt, etc., so to read integer input you will first read a String and then convert it to an integer using Integer.parseInt.

    You will do appropriate input validation. When choosing operations (in response to the "->" prompt) your program will only accept input as stated in the menu. When accepting integer input (line numbers, character positions in a line) your program will check for valid numbers and ranges.
    Attached Files Attached Files

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    What's the problem?
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    xquizitpinay is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    The problem is given above, that whole thing is an explanation of what the program has to do... I've attached what I've done so far with my code

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Yeah, but what is your question, do you have errors etc...? Or are we supposed to fix the whole thing. A lot of people will not take the time to read this if there isn't an exact question to be answered.
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by joshdgreen View Post
    A lot of people will not take the time to read this if there isn't an exact question to be answered.
    Agreed. And kudos to you for reading all that. I normally don't read a post that's more than a screenful high, unless there's a clear question in the first three lines or so.

    @OP: Learn how to get better help on a forum --
    How To Ask Questions The Smart Way

    db

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    That question reminds me of a little gem of a book "Software Engineering in RatFor" by Dennis Ritchie (there's also a Pascal version of it in which Dennis extensively discusses why Pascal is so bad); the book contains all the source code for the Unix 'ed' editor, including regular expressions and all. Maybe it can still be found in a dusty dark corner of a library. My copy of the book is not going to leave the house ;-)

    kind regards,

    Jos

    ps. RatFor was a C like pre-processor for Fortran; another language that never really made it ...

  7. #7
    xquizitpinay is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    the problem that I am having is that whenever i type in "nl" in the menu, for a new line, i type in a new line and that works fine, but whenever i tell it not to type in a new line it keeps repeating this line: "new line (y/n)" over and over again...

    could someone help me with this problem?

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    I'm going to take a shot in the dark, but maybe it is because inside the "newLine()" method you return "newLine()" which just keeps calling the same method over and over.
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    xquizitpinay is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    i've attached the changes i've made to my line editor java file... i've changed all my methods to "public void" methods so that they don't have to return anything....

    i'm having problems writing the lines that someone gives to a file... could someone take a look at that?
    Attached Files Attached Files

Similar Threads

  1. How do I run my program through Command Line?
    By s0meb0dy in forum New To Java
    Replies: 3
    Last Post: 10-03-2010, 01:13 AM
  2. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  3. Replies: 14
    Last Post: 01-21-2010, 09:10 AM
  4. Help on Editor Program
    By super3go in forum New To Java
    Replies: 4
    Last Post: 04-16-2009, 02:32 AM
  5. Calling a JFrame window from a command line program.
    By new_2_java in forum New To Java
    Replies: 7
    Last Post: 11-09-2008, 03:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •