-3

Reading in instructions.txt from instructions.java from a comma(,) delimited method for InstructionsController so that I may present it into a TextArea on Instructions.xml.

Having trouble figuring out once I've read in the file, how to pass over the ArrayList to another class to successfully use that data.

Using about in handle as a test button in the InstructionsController class.

Instructions.java

public class Instructions {

    private String[] identifier;

    private ArrayList<String> listName;
    private ArrayList<String> listDesc;

    public void dataLoader() {

        String fileName = "instructions.txt";
        Scanner scan;

        /*
         * Shows working path System.out.println(newFile(".").getAbsoluteFile());
         */

        try {
            scan = new Scanner(new File(fileName));
            // Iteration rather than iterable
            while (scan.hasNext()) {
                int i = 0;
                String line = scan.nextLine();
                identifier = line.split(">");

                if(identifier[i].equals("TITLE")) {
                    listName.add(identifier[i+1]);
                } else if (identifier[i].equals("DESC")) {
                    listDesc.add(identifier[i+1]);
                } else if (identifier[i].equals("END")) {
                     i++;
                }
            }
            scan.close();
        } catch (FileNotFoundException exception) {
            System.err.println("Failed to open instructions");
            System.exit(1);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ArrayList<String> getListName() {
        return listName;
    }

    public void setListName(ArrayList<String> listName) {
        this.listName = listName;
    }

    public ArrayList<String> getListDesc() {
        return listDesc;
    }

    public void setListDesc(ArrayList<String> listDesc) {
        this.listDesc = listDesc;
    }
}

InstructionsController.java

public class InstructionsController implements EventHandler<ActionEvent>, Initializable {

    @FXML
    private Button about, constants, professionPerks, teamCilantro, mainMenu, exit;

    @FXML
    private AnchorPane background;

    @FXML
    private TextArea text;

    Main main = new Main();

    private List<String> name;
    private List<String> desc;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Instructions instructions = new Instructions();
        name = instructions.getListName();
        desc = instructions.getListDesc();
    }

    @FXML
    public void handle(ActionEvent event) {
        Button selected = (Button) event.getSource();
        Stage stage = (Stage) selected.getScene().getWindow();

        if (selected == about)

        if(selected == mainMenu) 
            main.switchScene("fxml/Title.fxml", stage);

        if(selected == exit) 
            stage.close();
    }
}

sample text file input: instructions.txt

TITLE> Profession Perks:

DESC> Investor: Starts with $150 bonus "Starting Cash" for a total of $650 "Starting Cash". Farmer: Starts with 250 "Pounds of Food" and has a perk to consume food for the party at a 75% rate compared to normal. Handyman: Significantly less likelyhood of "Wagon" breaking down.

END>

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Conde
  • 21
  • 7

1 Answers1

0

You can attempt to create an instance of the first class in the second class file. Or you can attempt to set your 'get' methods to static. Take a look at this previous SO answer.

You need to create an instance of Class A and then access the values from Class B, for example:

public class ClassA {
  private int someInt;

  public ClassA() {
    someInt = 5;
  }

  public int getSomeInt() {
    return someInt;
  }
}

public class ClassB {
  public void someMethod() {
    ClassA instanceOfClassA = new ClassA();

    int someIntFromClassA = instanceOfClassA.getSomeInt();  //This is now 5;

    // Rest of your code here
  }
}

Alternatively you can create it as a static method in ClassA and then call it in ClassB by using ClassA.getSomeInt();

Passing an array from class A to class B

Jacq
  • 105
  • 2
  • 14