Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace string in text java

Posted on 2014-09-22
6
Medium Priority
?
258 Views
Last Modified: 2014-09-22
Hi:
I have a comma delimited text file and I want to replace one of the 'sections' with data from another.
I have written the process that collects the data as below but my problem is how to get the resulting string 'zz' into the proper place in the file (column 7 in the attached text file), replacing the existing text. Thus line 2 column 7 changes from
"20TKWIN/WIND FARM 00712" to "20-00712' as per array zz. (Yeah, line 1 is different - ignore that.)
Does anybody know how to do this?
Thanks!truemans.csv

Code to get the zz string :

    public static void main(String[] args) {
       
        /** amend file */
                 try {
             BufferedReader br = new BufferedReader(new FileReader("G:\\Sage50\\truemans.csv"));
            PrintStream psout = new PrintStream(new FileOutputStream("G:\\Sage50\\truemanshow.csv"));
                           String buff = null;
               while((buff=br.readLine()) != null){
                   String [] ss = buff.split(",");
                   String jobs = ss[6].substring(ss[6].length()-5);
                   String units = ss[6].substring(0,3);
                   String zz = units + "-" + jobs;
                    System.out.println(zz);
 
                              }

               br.close();
         

           } catch(Exception ex){
           }
0
Comment
Question by:ClaytonGlass
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 40336410
I would use a proper csv reader really but what's wrong with just writing the array you've derived from the line out to a new temp file after the manipulation?
0
 

Author Comment

by:ClaytonGlass
ID: 40336426
Thanks, CEHJ, I could write out the array to a new file - but it is parsing it into the correct 'column' that I am struggling with. I am guessing that I should create a series of arrays, each representing a column in the file, then slot  array 'zz' into the 7th point? Or is there another way? So build a file from a series of arrays?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 40336484
Thanks, CEHJ, I could write out the array to a new file - but it is parsing it into the correct 'column' that I am struggling with.
You're not - you've done that quite successfully ;) And you've made the change successfully. Now you just need to write out the whole array again (which would be greatly simplified by using a csv writer)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 40336487
       } catch(Exception ex){
           } 

Open in new window

See http://technojeeves.com/index.php/aliasjava1/94-ignoring-exceptions-is-dangerous
0
 

Author Closing Comment

by:ClaytonGlass
ID: 40336492
Thank you very much CEHJ. Please excuse my newbieness! I don't know what you mean by a csv writer but java has done the job! Thanks again.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40336547
http://ostermiller.org/utils/javadoc/CSVPrint.html

is the kind of thing i meant

You can also try

ss[6] =  ss[6].replaceAll("(^\\d{2}).*(\\d{5})", "$1-$2");

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question