0

What should be entered in intellij IDEA command line arguments? I have a file input.txt and output.txt (a matrix is ​​read from the first file, a new one is displayed in the second one) what should be passed to the command line arguments and how to do it syntactically correctly?

I have the program logic, it works in idea, but I need to run it via console, and that requires command line arguments.

package vsu.cs.vega;
import java.io.IOException;

public class Main {
public static void main(String[] args) throws IOException {
    Solution output = new Solution();
    String error = "You entered a non-rectangular matrix, please check the data and 
re-enter.";
    try {
        output.readMtx();
    }
    catch (ArrayIndexOutOfBoundsException exception){
        System.err.print(error);
    }


}

}

package vsu.cs.vega;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.ArrayIndexOutOfBoundsException;



public class Solution {

void readMtx() throws IOException {
    BufferedReader br = new BufferedReader(new 
FileReader("readInFileOutFromFile/src/vsu/cs/vega/input.txt"));

    ArrayList<String> lines = new ArrayList<>();
    while (br.ready()) {
        lines.add(br.readLine());
    }
    int matrixHeight = lines.size();

    int[][] matrix = new int[matrixHeight][];

    for(int i = 0; i < matrixHeight; ++i) {
        String[] nums = lines.get(i).split("\s*,\s*");
        matrix[i] = new int[nums.length];
        for(int j = 0; j < nums.length; ++j) {
            matrix[i][j] = Integer.parseInt(nums[j]);
        }
    }
    int[][] matrixForOutput = calc(matrix);
    try(PrintWriter out = new PrintWriter(new 
FileOutputStream("readInFileOutFromFile/src/vsu/cs/vega/output.txt"))) {
        for (int i = 0; i < matrixHeight; ++i) {
           out.println(Arrays.toString(matrixForOutput[i]).replaceAll("^\\[|]$", 
""));
        }
    }
    catch (ArrayIndexOutOfBoundsException ignored){
    }


    //out.println(Arrays.toString(matrixForOutput).replaceAll("^\\[|]$", ""));
}

int[][] calc(int[][] array) {
    int rows = array.length;
    int cols = array[0].length;
    boolean[] minRows = null, maxRows = null;
    boolean[] minCols = null, maxCols = null;

    Integer min = null;
    Integer max = null;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (null == min || array[i][j] < min) {
                minRows = new boolean[rows];
                minRows[i] = true;
                minCols = new boolean[cols];
                minCols[j] = true;
                min = array[i][j];
            } else if (array[i][j] == min) {
                minRows[i] = true;
                minCols[j] = true;
            }

            if (null == max || array[i][j] > max) {
                maxRows = new boolean[rows];
                maxRows[i] = true;
                maxCols = new boolean[cols];
                maxCols[j] = true;
                max = array[i][j];
            } else if (array[i][j] == max) {
                maxRows[i] = true;
                maxCols[j] = true;
            }
        }
    }

    int rowsToDelete = 0, colsToDelete = 0;
    for (int i = 0; i < rows; i++) {
        if (minRows[i] || maxRows[i]) {
            rowsToDelete++;
        }
    }
    for (int i = 0; i < cols; i++) {
        if (minCols[i] || maxCols[i]) {
            colsToDelete++;
        }
    }

    if (rows == rowsToDelete || cols == colsToDelete) {
        return new int[1][0];
    }

    int[][] result = new int[rows - rowsToDelete][cols - colsToDelete];

    for (int i = 0, r = 0; i < rows; i++) {
        if (minRows[i] || maxRows[i])
            continue; // пропустить строку, содержащую минимум или максимум
        for (int j = 0, c = 0; j < cols; j++) {
            if (minCols[j] || maxCols[j])
                continue; // пропустить столбец, содержащий минимум или максимум
            result[r][c++] = array[i][j];
        }
        r++;
    }
    //out.println(Arrays.toString(array).replaceAll("^\\[|]$", ""));

    return result;
}

}

here we need write args

vegaLDN
  • 29
  • 5

1 Answers1

0

Your program doesn't use the "String[] args" parameter of the main method. So you don't have to provide "Program arguments". In IntelliJ in "Run Configurations" you can leave the input field empty or enter what ever you want. It doesn't make much of a difference.

However for example you could pass path names of "input.txt" and "output.txt" via program arguments instead of hard coding them in your "readMtx" method.

Heiko Zelt
  • 21
  • 3
  • how can I run this file on the command line so that everything works. the fact is that when I want to run Main.class I get an error from the following article: https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – vegaLDN Nov 12 '22 at 20:07
  • First you should set the PATH environment variable. It depends on your operating system how to do it. Then call `java vsu.cs.vega.Main` – Heiko Zelt Nov 14 '22 at 15:09
  • ps: The file Main.class has to be in directory vsu/cs/vega/ relative to your current working directory. – Heiko Zelt Nov 14 '22 at 15:38