Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi everybody. I need java program which will print special range of lines from f

ID: 3598710 • Letter: H

Question

Hi everybody. I need java program which will print special range of lines from file using Regex (like ^ - beginning of file, $ - end of file).

For example: java Print 1.txt 10 15 - program should print lines from 10 to 15

java Print 1.txt ^ $ - program should print all lines from first to the end of file

java Print 1.txt 10 $ - print lines from 10 to the end java

Print 1.txt $ 10 - prints lines from the last to the 10

I don't know exactly how i can proceed range of lines with Regex (^, $...).

Thanks.

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Print {

   public static void main(String[] args) {

       if (args.length < 3) {
           System.out.println("Enter three arguments Print $1 $2 $3");
       }

       String fileName = args[0];
       String first = args[1];
       String last = args[2];
       List<String> fileContent = new ArrayList<String>();

       File file = new File(fileName);
       try {
           Scanner reader = new Scanner(file);
           while (reader.hasNextLine()) {
               fileContent.add(reader.nextLine());
           }

       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       int start = 0;
       int end = 0;
       if (first.equals("^")) {
           start = 1;
       } else if (first.equals("$")) {
           start = fileContent.size();
       } else {
           start = Integer.parseInt(first);
       }

       if (last.equals("^")) {
           end = 1;
       } else if (last.equals("$")) {
           end = fileContent.size();
       } else {
           end = Integer.parseInt(last);
       }

       // ^ $ --> DONE
       // 10 15 --> DONE
       // 10 $ --> DONE
       // $ 10 --> DONE
       // $ ^ --> DONE
       // System.out.println(start + ":" + end);
       if (start < end) {
           for (int i = start - 1; i < end;) {
               System.out.println(fileContent.get(i));
               i++;
           }

       }else {
           for (int i = start-1; i >= end-1;) {
               System.out.println(fileContent.get(i));
               i--;
           }
       }
   }

}


============

java Print 1.txt ^ $
first Line
2nd Line
3rd Line
4th Line
5th Line
6th Line
7th Line
8th Line
9th Line
10th Line
11th Line
12th Line
13th Line
14th Line
15th Line
16th Line
Last Line

java Print 1.txt 10 15
10th Line
11th Line
12th Line
13th Line
14th Line
15th Line

java Print 1.txt 10 $
10th Line
11th Line
12th Line
13th Line
14th Line
15th Line
16th Line
Last Line

java Print 1.txt $ ^
Last Line
16th Line
15th Line
14th Line
13th Line
12th Line
11th Line
10th Line
9th Line
8th Line
7th Line
6th Line
5th Line
4th Line
3rd Line
2nd Line
first Line


========

INPUT:-

============

first Line
2nd Line
3rd Line
4th Line
5th Line
6th Line
7th Line
8th Line
9th Line
10th Line
11th Line
12th Line
13th Line
14th Line
15th Line
16th Line
Last Line

========T

Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote