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.
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.
Comment