cd anyone help me
i need to find a pattern in a file and replace it with new value
the pattern is a double
19 1739
cd anyone help me
i need to find a pattern in a file and replace it with new value
the pattern is a double
The word 'double' or just any double?
If it's any double and you're reading stuff in from a file, you would need to set up a Scanner, and then go line by line looking for [number].[number] .
a double i need to find a double in a file and replace it with a new value
how to replace it ?
You could read the entire file into a String, find the double and replace it, and then rewrite the original file with the altered String.
You could read the entire file into a String, find the double and replace it, and then rewrite the original file with the altered String.
If you read the line in (and here is where I hope that I remember correctly that were in the Java forum) you can do a Pattern.match() on a pre-compiled regex, or if you wanted to use strings, you can do String.indexOf() for the period and test both sides for numbers.
a double i need to find a double in a file and replace it with a new value
how to replace it ?
Text files don't have data types. When you read from a Java program is when you choose which data type you want to read the values as. If you use scanner, you can read as double directly but since you say that you want to replace the double then you should the data as strings because Patterns are strings. Do you know the expression for a decimal number then? You might need to have a look at a regex tutorial first if you don't.
why it highlights the Pattern.compile(patternStr); and
pattern.matcher(wczytane_dane); as an error
also is [0-9]{0,2}\\.{0,1}[0-9]{0,2} good for a double -
-
private void write_to_file(String wplata_wyplata){
-
try{
-
-
-
-
BufferedReader in = new BufferedReader(new FileReader(filename));
-
-
-
-
String wczytane_dane="";
-
String line = in.readLine();
-
-
-
-
double sd= sr_dost;
-
Double a1 = new Double(sd);
-
-
while((line = in.readLine()) != null) {
-
wczytane_dane+=line;
-
line = in.readLine();
-
-
}
-
-
String patternStr = "[0-9]{0,2}\\.{0,1}[0-9]{0,2}";
-
String replacementStr = a1.toString();;
-
-
-
Pattern pattern = Pattern.compile(patternStr);
-
-
-
Matcher matcher = pattern.matcher(wczytane_dane);
-
String output = matcher.replaceAll(replacementStr);
-
-
-
-
FileWriter fstream = new FileWriter("konta//"+NumerKonta+".txt");
-
BufferedWriter out = new BufferedWriter(fstream);
-
-
-
// write wczytane_dane to a file will be here
-
-
-
-
-
out.close();
-
-
-
-
-
-
in.close();
-
}catch (FileNotFoundException e) {
-
System.err.println("Nie ma takiego konta(File not found!)");
-
}catch (IOException e) {
-
System.err.println(e);
-
} catch (NumberFormatException e) {
-
System.out.println("NumberFormatException: " + e.getMessage());
-
}catch (Exception ex) {
-
ex.printStackTrace(); }
-
-
-
}
-
-
why it highlights the Pattern.compile(patternStr); and
pattern.matcher(wczytane_dane); as an error
also is [0-9]{0,2}\\.{0,1}[0-9]{0,2} good for a double -
-
private void write_to_file(String wplata_wyplata){
-
try{
-
-
-
-
BufferedReader in = new BufferedReader(new FileReader(filename));
-
-
-
-
String wczytane_dane="";
-
String line = in.readLine();
-
-
-
-
double sd= sr_dost;
-
Double a1 = new Double(sd);
-
-
while((line = in.readLine()) != null) {
-
wczytane_dane+=line;
-
line = in.readLine();
-
-
}
-
-
String patternStr = "[0-9]{0,2}\\.{0,1}[0-9]{0,2}";
-
String replacementStr = a1.toString();;
-
-
-
Pattern pattern = Pattern.compile(patternStr);
-
-
-
Matcher matcher = pattern.matcher(wczytane_dane);
-
String output = matcher.replaceAll(replacementStr);
-
-
-
-
FileWriter fstream = new FileWriter("konta//"+NumerKonta+".txt");
-
BufferedWriter out = new BufferedWriter(fstream);
-
-
-
// write wczytane_dane to a file will be here
-
-
-
-
-
out.close();
-
-
-
-
-
-
in.close();
-
}catch (FileNotFoundException e) {
-
System.err.println("Nie ma takiego konta(File not found!)");
-
}catch (IOException e) {
-
System.err.println(e);
-
} catch (NumberFormatException e) {
-
System.out.println("NumberFormatException: " + e.getMessage());
-
}catch (Exception ex) {
-
ex.printStackTrace(); }
-
-
-
}
-
-
Did you import the java.util.regex package?
should be -
String output = matcher.replaceFirst(replacementStr);
-
there
i have imported
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import com.sun.org.apache.xalan.internal.xsltc.compiler.P attern;
should be -
String output = matcher.replaceFirst(replacementStr);
-
there
I'm sorry I don't get it. Is it a question or have you got it to work?
no i havent it still highlights Pattern.compile(patternStr); (highlights the word compile)and pattern.matcher(wczytane_dane); (highlights the word matcher)as an error
i have imported
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import com.sun.org.apache.xalan.internal.xsltc.compiler.P attern;
eclipse doesnt say that i need to import some other package
no i havent it still highlights Pattern.compile(patternStr); (highlights the word compile)and pattern.matcher(wczytane_dane); (highlights the word matcher)as an error
i have imported
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import com.sun.org.apache.xalan.internal.xsltc.compiler.P attern;
eclipse doesnt say that i need to import some other package
What is the error it is giving you - can you copy and paste it here?
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
The method compile(ClassGenerator, MethodGenerator) in the type Expression is not applicable for the arguments (String)
The method matcher(String) is undefined for the type Pattern
at konta_bankowe.Konto.write_to_file(Konto.java)
at konta_bankowe.Konto.depozyt(Konto.java)
at konta_bankowe.Konto.access$2(Konto.java)
at konta_bankowe.Konto$2.actionPerformed(Konto.java)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
This probably won't change anything, but can you try real quick for me to change your import to import java.util.regex.*; for me?
i changed the import (4u) and i still get the highlights
i changed the import (4u) and i still get the highlights
Remove - import com.sun.org.apache.xalan.internal.xsltc.compiler.P attern;
-
Just use the standard Java API with import java.util.regex.*;
it fixed the prob
but it doesnt replace a double in a string is this pattern correct for a double "[0-9]{0,2}\\.{0,1}[0-9]{0,2}"; and it reads in every second line not every line i checked it with system.out -
private void write_to_file(String wplata_wyplata){
-
try{
-
-
-
-
BufferedReader in = new BufferedReader(new FileReader("konta//"+NumerKonta+".txt"));
-
-
-
-
String wczytane_dane="";
-
String line = in.readLine();
-
-
double sd= sr_dost;
-
Double a1 = new Double(sd);
-
-
while((line = in.readLine()) != null) {
-
wczytane_dane+=line;
-
line = in.readLine();
-
wczytane_dane+='\n';
-
-
}
-
-
String patternStr = "[0-9]{0,2}\\.{0,1}[0-9]{0,2}";
-
String replacementStr = a1.toString();;
-
-
-
Pattern pattern = Pattern.compile(patternStr);
-
-
-
Matcher matcher = pattern.matcher(wczytane_dane);
-
String output = matcher.replaceFirst(replacementStr);
-
System.out.println("output ze zmienionym balansem"+output);
-
-
in.close();
-
//here will be writing to a file
-
}catch (FileNotFoundException e) {
-
System.err.println("Nie ma takiego konta(File not found!)");
-
}catch (IOException e) {
-
System.err.println(e);
-
} catch (NumberFormatException e) {
-
System.out.println("NumberFormatException: " + e.getMessage());
-
}catch (Exception ex) {
-
ex.printStackTrace(); }
-
-
-
}
-
-
it fixed the prob
but it doesnt replace a double in a string is this pattern correct for a double "[0-9]{0,2}\\.{0,1}[0-9]{0,2}"; and it reads in every second line not every line i checked it with system.out -
while((line = in.readLine()) != null) {
-
wczytane_dane+=line;
-
line = in.readLine();
-
}
-
You have two readlines - that's your 'every-other line' problem
As far as the regex goes - does the period need to be backwhacked?
I would think it would be something like [0-9]+.[0-9]+ just quick and rough, that's at least one 0-9, a period (not sure if this needs to be backwhacked or not), and at least one 0-9 digit, but could be any amount.
The way you have it, you pick up a number between 10 and 99 whose second digit is two or zero, and then three places after that. Is that what you want?
thank u guys u r so helpful i dont know how i cd overlook that i had 2 readlines
... it finds and replaces the double now
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michael McGarry |
last post by:
Hi,
I am horrible with Regular Expressions, can anyone recommend a book on it?
Also I am trying to parse the following string to extract the number
after load average.
".... load average:...
|
by: Kenneth McDonald |
last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate
feedback, suggestions, and criticism as I work towards finalizing the
API and feature sets. rex is a module intended to make...
|
by: Sehboo |
last post by:
Hi,
I have several regular expressions that I need to run against
documents. Is it possible to combine several expressions in one
expression in Regex object. So that it is faster, or will I...
|
by: Együd Csaba |
last post by:
Hi All,
I'd like to "compress" the following two filter expressions into one -
assuming that it makes sense regarding query execution performance.
.... where (adate LIKE "2004.01.10 __:30" or...
|
by: Billa |
last post by:
Hi,
I am replaceing a big string using different regular expressions (see
some example at the end of the message). The problem is whenever I
apply a "replace" it makes a new copy of string and I...
|
by: a |
last post by:
I'm a newbie needing to use some Regular Expressions in PHP.
Can I safely use the results of my tests using 'The Regex Coach'
(http://www.weitz.de/regex-coach/index.html)
Are the Regular...
|
by: Mike |
last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in
matches. I would like to get what the actual regular expression is.
In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
|
by: Allan Ebdrup |
last post by:
I have a dynamic list of regular expressions, the expressions don't change
very often but they can change. And I have a single string that I want to
match the regular expressions against and find...
|
by: Wiseman |
last post by:
I'm kind of disappointed with the re regular expressions module. In
particular, the lack of support for recursion ( (?R) or (?n) ) is a
major drawback to me. There are so many great things that can...
|
by: FAQEditor |
last post by:
Anybody have any URL's to tutorials and/or references for Regular
Expressions?
The four I have so far are:
http://docs.sun.com/source/816-6408-10/regexp.htm...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |