Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing a filename as a argument in Java

i started learning java for a couse in my university. Im having a bit of problems on this challenge. I want to pass the filename lets say num.txt as a argument (javac Ex1.java "filename"). Then use that file to do some operations. The code right now is as such:

import java.io.*;
import java.net.URI;
import java.util.*;
import java.nio.file.*;
import java.nio.charset.*;

public class Ex1{
    public static void main(String[] args){
    

    try{
        Charset ENCODING = StandardCharsets.UTF_8;
        Path path = Paths.get("num.txt");
        List<String> linhas = Files.readAllLines(path,ENCODING);
        for(int i=0;i<linhas.size();i++){
            System.err.println("Linhas n" + i+":"+linhas.get(i));
        }
        int num1 = Integer.parseInt(linhas.get(0));
        int num2 = Integer.parseInt(linhas.get(1));
        System.out.println(num1+num2);
        System.out.println(num1-num2);
        System.out.println(num1*num2);
        System.out.println(num1/num2);
        

    }catch(IOException ex){
        System.out.println("Erro!");
    }
}
}

I would like some documentation or help. Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you’re passing a filename as an argument you should use args[0] to retrieve it

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading