Results 1 to 11 of 11
- 04-03-2011, 08:40 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Deleting specific chars from a file
Hi all,
I have the following code which is reading a text file, there are some dashes (-) I would like to take them out such as (PRODUCT-NAME-IN) to be (PRODUCTNAMEIN) as an example. Could anyone help me with it please.
Thanks in advance ,,Java Code:import java.io.*; import java.util.Calendar; public class ReadAndWrite{ public static void main(String args[]) throws IOException { FileInputStream ProductID = new FileInputStream("javaOutput.java"); BufferedReader ProductIDinStream = new BufferedReader(new InputStreamReader(ProductID)); PrintWriter(new File("Product.dat"))); String line1; int i; for (i=0; i<3 ; i++ ){ line1=ProductIDinStream.readLine(); if (line1.equals("PRODUCT-NAME-IN")) line1.replaceAll("PRODUCT-NAME-IN", "R"); System.out.println(line1); } { System.out.println("Rjnjjaid"); } ProductIDinStream.close(); } }
- 04-03-2011, 09:10 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
replaceAll returns a new String and does not change the old one (because of Strings are immutable) so you have to write line1 = line1.replaceAll....if (line1.equals("PRODUCT-NAME-IN"))
line1.replaceAll("PRODUCT-NAME-IN", "R");
System.out.println(line1);
And why dou want to replace it with a R ?
System.out.println("PRODUCT-NAME-IN".replace("-", ""));I would like to take them out such as (PRODUCT-NAME-IN) to be (PRODUCTNAMEIN) as an example
or more generally
System.out.println("PRODUCT-NAME-IN".replaceAll("(.*?)-(.*?)-(.*?)", "$1$2$3"));
:D :confused:
- 04-03-2011, 09:16 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Perfect,, it works very well,,
Thank you so much for your help,, BTW I was writing R just to try it,,
- 04-03-2011, 09:18 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
I'm sorry but how can I make it general.. i have many places that have dashes and I would like to change all of them.. what should I write instead of PRODUCT-NAME-IN..
Thanks in advance for your help
- 04-03-2011, 09:25 AM #5
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
We dont know how your file/line looks like, so we cant give you the right answer.
But if (line1.equals("PRODUCT-NAME-IN")) only returns true if the line is 1:1 like "PRODUCT-NAME-IN"
You could use contains("-") to check if the line contains a minus sign?! Or you could use matches(..) with a correct regular expression ?!
Or Pattern and Matcher classes, or the Scanner class..or or or :)
Can you give us an example how your file looks like?
- 04-03-2011, 09:34 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Here I have listed a part of my text file.. I would like to make it general, so that will read until find any dash and then delete this dash.
the line that you gave it to me earlier was working perfectly with just "PRODUCT-NAME-IN" but I would like to make it general for all the dashes..
----------------------- Starting the part of the text file -------------------
BufferedReader PRODUCT-NAME-IN = new BufferedReader
BufferedReader PRODUCT-IN-STOCK-IN = new BufferedReader
BufferedReader PRODUCT-SALES-PRICE-IN = new BufferedReader
BufferedReader SUPPLIER-COMPANY-NAME-IN = new BufferedReader
BufferedReader SUPPLIER-PHONE-IN = new BufferedReader
String PRODUCT-NAME-IN-IN = 01.readLine();
String P-STOCK-IN = 01.readLine();
String P-SALES-IN = 01.readLine();
S-ID-OUT = RAND-NUMBER + S-ID-OUT;
S-ID-OUT = P-ID-OUT + S-ID-OUT;
P-ID-OUT = S-ID-OUT + P-ID-OUT;
int PID = P-ID-OUT / CURRENT-9;
float P-ID-OUT = P-ID-OUT1 % CURRENT-9;
int SID = S-ID-OUT / PID;
float S-ID-OUT = S-ID-OUT1 % PID;
PRODUCT-O-OUT.Write ("\t" + "hello" + "\t");
PRODUCT-O-OUT.Write ("\t" + "Hello2" + "\t");
PRODUCT-NAME-IN.close();
PRODUCT-IN-STOCK-IN.close();
PRODUCT-SALES-PRICE-IN.close();
PRODUCT-OUT.close();
SUPPLIER-COMPANY-NAME-IN.close();
SUPPLIER-PHONE-IN.close();
----------------- end of the example---------------
Hope that make it easier,,
- 04-03-2011, 04:46 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Hi everyone,
Is there anyone can help me please???
Thanks in advance
- 04-03-2011, 04:48 PM #8
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
I want to help, but I dont understand your previous post and dont understand what problem do you have right now.
- 04-03-2011, 04:57 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Thank you so much .. let's say I have the following text file
----------------------- Starting the part of the text file -------------------
BufferedReader PRODUCT-NAME-IN = new BufferedReader
BufferedReader PRODUCT-IN-STOCK-IN = new BufferedReader
BufferedReader PRODUCT-SALES-PRICE-IN = new BufferedReader
BufferedReader SUPPLIER-COMPANY-NAME-IN = new BufferedReader
BufferedReader SUPPLIER-PHONE-IN = new BufferedReader
String PRODUCT-NAME-IN-IN = 01.readLine();
String P-STOCK-IN = 01.readLine();
String P-SALES-IN = 01.readLine();
S-ID-OUT = RAND-NUMBER + S-ID-OUT;
S-ID-OUT = P-ID-OUT + S-ID-OUT;
P-ID-OUT = S-ID-OUT + P-ID-OUT;
int PID = P-ID-OUT / CURRENT-9;
float P-ID-OUT = P-ID-OUT1 % CURRENT-9;
int SID = S-ID-OUT / PID;
float S-ID-OUT = S-ID-OUT1 % PID;
PRODUCT-O-OUT.Write ("\t" + "hello" + "\t");
PRODUCT-O-OUT.Write ("\t" + "Hello2" + "\t");
PRODUCT-NAME-IN.close();
PRODUCT-IN-STOCK-IN.close();
PRODUCT-SALES-PRICE-IN.close();
PRODUCT-OUT.close();
SUPPLIER-COMPANY-NAME-IN.close();
SUPPLIER-PHONE-IN.close();
----------------- end of the example---------------
and I would like to to change every dash(-) in this text file to be without all the dashes.
I used your following solution and it works perfectly but it works with a specific variable or statement.
--------- Here is your helpful solution ------------------
System.out.println("PRODUCT-NAME-IN".replace("-", ""));
or more generally
System.out.println("PRODUCT-NAME-IN".replaceAll("(.*?)-(.*?)-(.*?)", "$1$2$3"));
--------------- end of your solution --------------------
This solution helps a lot with a specific variable ("PRODUCT-NAME-IN") but I would like to make more general and read everything but once face a DASH delete it directly..
Hope I explain it correctly..
- 04-03-2011, 05:20 PM #10
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
....
:confused:Java Code:public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new File("YOURTEXTFILE.txt")); while(sc.hasNextLine()){ System.out.println(sc.nextLine().replace("-", "")); } }
- 04-03-2011, 05:25 PM #11
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Intaking String and splitting into chars, returning individual chars as string array
By Gokul138 in forum New To JavaReplies: 1Last Post: 02-07-2011, 08:22 PM -
JTextfield only allow specific chars
By fuzzdn in forum AWT / SwingReplies: 1Last Post: 12-11-2010, 03:49 PM -
File Not Deleting
By Moncleared in forum New To JavaReplies: 7Last Post: 02-21-2010, 08:28 PM -
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 10:40 AM -
Help deleting a file
By 3speed in forum New To JavaReplies: 4Last Post: 11-01-2008, 05:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks