473,699 Members | 2,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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.
Mar 6 '08 #1
1 1637
sukatoa
539 Contributor
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
Mar 6 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1905
by: ???????J | last post by:
Javascript may inquire the push down menu value, can I inquire the description? The following example, the variable($answer) can be get the menu1's value. For example, if I select first data, the menu1's value would be 1. Therefore ($answer) would be 1. But If want using javascript get the description(ABC),which javascript command can do it?
8
3382
by: DKM | last post by:
Here are the source code files to a Java applet that utilizes LiveConnect to communicate with Javascript, and the HTML file. The thing works both in IE 6.0 and FireFox 1.4. but with some problems. IE crashes when one refreshes the page or leave the page. This happens only after calling the Java method more than once. It does not crash if the Java method is called just once and then the page is refreshed. FireFox does not crash at all...
2
10695
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
24
4381
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what we covered was within the last week of the class and all self taught. Our prof gave us an example of a Java method used to remove elements from an array: public void searchProcess() { int outIt=0;
6
1825
by: chris_fieldhouse | last post by:
Hi, I have a script for processing emails, The script finds email sent to a particular alias, grabs the body text of the email and stores it into a database. Problem is that certain character like '_' sometimes get stored as =5F, and some email clients seem to add in =0D encoded characters. I store the text in an Mysql database, and depending on the message
0
5569
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
3
4238
by: Simon Brooke | last post by:
As various people will have noticed, I've been having a lot of trouble with XSL lately. Brief history: I wrote myself an XML toolkit back in 2000, and it worked well enough for me, so it's been little changed since. However, it works only with an obsolete version of Saxon (6.2.2 I think), and it has a number of small bugs; and I've at last got to the point where I need to fix it. And I'm finding it, frankly, absurdly frustrating and...
4
1235
by: civicjai | last post by:
Can anyone teach me how to An item code is to be entered by the user. If it is an existing item code that matches an item in the items array , a description of the item with its unit price is displayed. For example, if the user types the item code “1000” and then presses the Enter key,“low-fat milk (1 litre)/$2.15” is displayed on the screen, followed by another prompt for typing the next item code. And also,If the entered item code...
58
8087
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also the parts section an i seem to be having trouble. When i try to insert into the parts section i get the error Invalid character value for cast specification. But not sure what i am doing wrong. Here is what i am using to insert. All the sections...
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.