Help with removing only certain parts of text from a .txt document using java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Computernut234
    New Member
    • Mar 2008
    • 1

    #1

    Help with removing only certain parts of text from a .txt document using java

    Hi, i'm doing a project for my Java class and I know how to add text to a .txt document and remove the entire text but I do not know how to only remove certain parts of text. My code is supposed to allow users to add/remove text depending on the condition.

    The code is



    import java.io.*;
    import java.util.*;

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

    int maxIndx = 0;
    String array[]= new String[10];
    Scanner sc = new Scanner(new File("C:\\text\ \text1.txt"));
    while(sc.hasNex t())
    {//open while 3

    maxIndx++;
    array[maxIndx] = sc.nextLine();

    }//close while 3

    for(int j = 0; j<array.length ; j++)
    {
    System.out.prin tln(array[j]);
    }
    sc.close();

    System.out.prin tln("");

    System.out.prin tln("REMOVE:");

    FileWriter fw = new FileWriter("C:\ \text\\text1.tx t");
    PrintWriter nw = new PrintWriter(fw) ;

    for(int k = 0; k<array.length ; k++)
    {

    if(array[k] == "jacob")
    {
    break;
    }
    }
    fw.close();
    nw.close();
    }
    }

    and it is supposed to interact with a folder int he C: drive called text with the text inside being (in this exact order)

    text doc
    _____________
    Jacob is a bad
    person and
    he must
    be punished.<<<<<< <<<<< part being removed with everything else remaining.

    I know i'm supposed to use an array to make the program basically remove everythign and than re add it without putting the be punished part back in but i'm stuck as to how to set it up. If anyone can assist i would greatly appreciate it.
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by Computernut234
    Hi, i'm doing a project for my Java class and I know how to add text to a .txt document and remove the entire text but I do not know how to only remove certain parts of text. My code is supposed to allow users to add/remove text depending on the condition.

    The code is



    import java.io.*;
    import java.util.*;

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

    int maxIndx = 0;
    String array[]= new String[10];
    Scanner sc = new Scanner(new File("C:\\text\ \text1.txt"));
    while(sc.hasNex t())
    {//open while 3

    maxIndx++;
    array[maxIndx] = sc.nextLine();

    }//close while 3

    for(int j = 0; j<array.length ; j++)
    {
    System.out.prin tln(array[j]);
    }
    sc.close();

    System.out.prin tln("");

    System.out.prin tln("REMOVE:");

    FileWriter fw = new FileWriter("C:\ \text\\text1.tx t");
    PrintWriter nw = new PrintWriter(fw) ;

    for(int k = 0; k<array.length ; k++)
    {

    if(array[k] == "jacob")
    {
    break;
    }
    }
    fw.close();
    nw.close();
    }
    }

    and it is supposed to interact with a folder int he C: drive called text with the text inside being (in this exact order)

    text doc
    _____________
    Jacob is a bad
    person and
    he must
    be punished.<<<<<< <<<<< part being removed with everything else remaining.

    I know i'm supposed to use an array to make the program basically remove everythign and than re add it without putting the be punished part back in but i'm stuck as to how to set it up. If anyone can assist i would greatly appreciate it.
    If i where to implement that, retrieve the text first from the .txt file to your storage...

    You can now remove the text inside the storage, and write it again to a file...
    Writing file with the same name overwrites the previous file...

    I hope it helps,
    Sukatoa

    Comment

    Working...