1

I have a csv file with 5 numbers in each line.

a line in csv:- 10003478,12228,7711479,15013889,3070120,5834262

I need to convert this string into something like this

expected:- 10003478,2/28/12228,4/79/7711479,8/89/15013889,1/20/3070120,2/62/5834262

In the expected output, the numbers around '/' are the last three digits of each number.

Eg: 12228 - last three digits are 228 which are separated like that and made into 2/28/12228

3
  • Parse it (opencsv.sourceforge.net), transform it, write it back to file. Commented May 27, 2016 at 8:50
  • Your not asking us to help you solve the problem? :) why is the first number special? Commented May 27, 2016 at 8:50
  • @MinhKieu, no I'm not. It's just that I'm unable to understand where to start from. Actually there are a few more checks to be done along with above operations. A pointer where to start from would help me a lot. First number is a special code which don't need transformation. Commented May 27, 2016 at 8:53

3 Answers 3

1

You can use the replaceAll method and save it into a new String:

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

    public static void main(String[] args) throws IOException {
        // Read file
        Path filePath = Paths.get("file.txt");
        byte[] arr = Files.readAllBytes(filePath);
        String content = new String(arr, Charset.defaultCharset());

        // Replace
        String newContent = new String(
                content.replaceAll(",([0-9]+)([0-9]{1})([0-9]{2})", ",$2/$3/$1$2$3"));

        System.out.println(newContent);
    }
}

This produces:

10003478,2/28/12228,4/79/7711479,8/89/15013889,1/20/3070120,2/62/5834262
Sign up to request clarification or add additional context in comments.

Comments

0

You could have a pattern like this:

,((\d+)(\d)(\d{2}))

and do a replaceAll with:

,$3/$4/$1

Example in javascript:

console.log("10003478,12228,7711479,15013889,3070120,5834262".replace(/,((\d+)(\d)(\d{2}))/g, ",$3/$4/$1"));

Comments

0

I do not see why you should use regex. Instead you can just read the file and convert the strings and write back to file. Below is an example how it could look like using OpenCsv

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


public class JavaApplication22 {

   public static void main(String[] args) throws IOException { 
       String path = "C:\\Users\\uzochi\\desktop\\numbers.csv";
       CSVReader reader = new CSVReader(new FileReader(path));
       List<String[]> myEntries = reader.readAll();

       for(String[] s : myEntries){
           for(int i = 1; i<=5; i++){
              String last3 = s[i].substring(s[i].length()-3,s[i].length()); 
              last3 = last3.charAt(0)+"/"+last3.substring(1)+"/";
              s[i] = last3+s[i];               
           }
           System.out.println(Arrays.toString(s));
       }

       CSVWriter writer = new CSVWriter(new FileWriter(path), ','); 
       for(String[] s : myEntries){       
          writer.writeNext(s);
       }
   writer.close();
   }    
 }

about openCsv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.