'Why is The Constructor Expecting a class When I put an Array as a Parameter - JavaFx [closed]
I am working on a javaFX project that expects the user to press a button to store information inputed on the text fields. What is stumping me is how to pass the String values that I have collected from the text fields into the other class, DisplayStage, that I made to display the stage of the values input by the user. My instructor, says that I am supposed to pass the array of the strings by using the constructor of the other class I created, DisplayStage. This is what I tried:
btnAdd.setOnAction((e) -> {
// i keeps count of how many times the button was pushed
this.i++;
//Setting the size of the array to be as big as how many times it was clicked
String [] dataEntries = new String [this.i];
//Inside info, we will store the Data inputed by the user
info = txtID.getText() + " , " + txtName.getText() + " , " + txtAdress.getText() + " , " + txtCity.getText();
// At index i-1 of dataEntries[], store the new entry stored insode of info
dataEntries[i-1] = info;
DisplayStage saveNewEntry = new DisplayStage(dataEntries[]);
});
the constructor in the DisplayStage class expects a String array.
What I found out from putting this into practice is that, constructors are not like methods and you cannot pass an array like this, instead, by looking at the compilation error message I know that I am supposed to reference a .class. I might be missing something like a special Syntax that I can use to pass the array to the constructor or maybe I should create a class that stores the data from the user and then reference that class inside the constructor?
Thank you.
Solution 1:[1]
You can pass arrays in constructors just like any other objects by name:
DisplayStage saveNewEntry = new DisplayStage(dataEntries);
And your constructor can be defined like this:
public DisplayStage(String[] values) {
// Your code
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eugene |
