aysu kaplan
aysu kaplan

Reputation: 3

Reaching command line arguments in another class

A have four classes: Main, Read, Author, Commands. In Read class:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class Read {
     static ArrayList<String> arAuthor = new ArrayList<>();
     static ArrayList<String> arCommand = new ArrayList<>();

public static ArrayList<String> getArAuthor() {
    return arAuthor;
}

public static void setArAuthor(ArrayList<String> arAuthor) {
    Read.arAuthor = arAuthor;
}

public static ArrayList<String> getArCommand() {
    return arCommand;
}

public static void setArCommand(ArrayList<String> arCommand) {
    Read.arCommand = arCommand;
}

public static void main(String[] args) {
    BufferedReader author;
    BufferedReader command;
    String thisLine;
    String thisLine1;
    try {
        author = new BufferedReader(new FileReader(args[0]));
        command = new BufferedReader(new FileReader(args[1]));
        while ((thisLine = author.readLine()) != null) {
            System.out.println(thisLine);
            arAuthor.add(thisLine);
        }
        while ((thisLine1 = command.readLine()) != null) {
            System.out.println(thisLine1);
            arCommand.add(thisLine1);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

My code works as Read.java args[0] args[1] but i want it to work like Main.java args[0] args[1]. I am new to Java so ı can't figure how can i pass this arguments to Main.java

Upvotes: 0

Views: 191

Answers (1)

Akif Hadziabdic
Akif Hadziabdic

Reputation: 2890

Solution:

public class Reader {
    public List<String> arAuthor = new ArrayList<>();
    public List<String> arCommand = new ArrayList<>();

    public void read(String first, String second) throws IOException {
        String thisLine;
        String thisLine1;
        try (BufferedReader author = new BufferedReader(new FileReader(first));
             BufferedReader command = new BufferedReader(new FileReader(second));){
            while ((thisLine = author.readLine()) != null) {
                System.out.println(thisLine);
                arAuthor.add(thisLine);
            }
            while ((thisLine1 = command.readLine()) != null) {
                System.out.println(thisLine1);
                arCommand.add(thisLine1);
            }
        }
    }
}

public class Main {
    public static void main(String[] args) throws IOException {
        Reader reader = new Reader();
        reader.read(args[0], args[1]);
        System.out.println(reader.arAuthor);
        System.out.println(reader.arCommand);
    }
}

Upvotes: 1

Related Questions