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>