0

Need help on solving below usecase using Java where in the below paragraph

input

A@123@456789@10111213
B@123@456789
C@123@456789@101112131415
D@123@456789
E@123@456789@101112131415161718

has to be changed to this way below

output

A@123@4567
89@1011121
3
B@123@4567
89
C@123@4567
89@1011121
31415
D@123@4567
89
E@123@4567
89@1011121
3141516171
8

meaning each line in the given paragraph is checked for a certain length in this case 10 , and if it exceeds a newline has to be added. This behavior has to be applied on every line in the paragraph

Any help ?

Nis
  • 27
  • 10
  • What exactly do you mean by "paragraph"? – k314159 Apr 29 '22 at 11:32
  • And in the output file, where each line is split into a number of lines, how can you then tell if a certain line is the start of a new line from the original file, or a continuation of the previous long line? – k314159 Apr 29 '22 at 11:34
  • to be more precise , you could say a file that contains 'n' number of lines. So each line need to check for certain length and if that exceeds a given size , a new line should be introduced – Nis Apr 29 '22 at 11:35
  • from the input file , you can assume like A*, B* etc are all of segment identifiers or line identifiers . Does this answer your query ? And every line ends with "\n" in the input file – Nis Apr 29 '22 at 11:38
  • Please [edit] the question update it with your own attempt. Also, please use text instead of images. – Smile Apr 29 '22 at 11:40
  • You can get some hints from this question: https://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java – k314159 Apr 29 '22 at 11:46

1 Answers1

1

You can achieve it like this

public static void main(String[] args) throws IOException {
            
            List<String> lines = Files.readAllLines(Paths.get("C:\\Path\\to\\File\\Test.txt"), StandardCharsets.US_ASCII);
            for(String s : lines) {
                int count=s.length()/10;
                if(s.length()%10!=0) {
                    count=(s.length()/10)+1; 
                }
                for(int i=0;i<count;i++) {
                    if(((i+1)*10)>s.length()) {
                        System.out.println(s.substring(i*10,s.length()));
                    }else
                    System.out.println(s.substring(i*10,(i+1)*10));
                }
            }
        
        }

By this way, you can split the file contents into your desired length.

Umeshwaran
  • 627
  • 6
  • 12