Reputation: 103
I have this class in android, i have connected it to xml layout that get user input for name, age, height and weight.
public class Person {
String name;
int age;
int height;
int weight;
}
I want the user to create a new person. I have try to get user input to create an object bu i got an error if i write this code.
Person editText.getText().toString() = new Person();
What is the correct way to allow the user to create a new person ?
Upvotes: 1
Views: 1189
Reputation: 69
Person editText.getText().toString() = new Person();
In this line code, your dynamic creating reference of an object but not storing value into object from edittext.
There are many ways to store object by using
Setter and getter injection
Person person = new Person(); person.setAge(AgeeditText.getText().toString()); person.setHeight(heighteditText.getText().toString()); person.setName(NameeditText.getText().toString()); person.setWeight(WeighteditText.getText().toString());
constructor injection
Person person = new Person(NameeditText.getText().toString(),AgeeditText.getText().toString(), ...);
....etc
Happy Coding
Upvotes: 0
Reputation: 1382
So first off, Person editText.getText().toString() = new Person();
is not valid syntax.
You are able to do String myPersonString = editText.getText().toString();
. Which would create a local variable named myPersonString
with the type String
. Because the Person class has multiple properties, it wouldn't make much sense to only have a single EditText
field for a person, you might have to have multiple EditText
fields.
private EditText nameText, ageText, heightText, weightText;
// initialize these in your onCreate() like you're already doing with editText
Now that we have those set up, we can get their values.
String name = nameText.getText().toString();
try{
int age = Integer.parseInt(ageText.getText().toString());
int height = Integer.parseInt(heightText.getText().toString());
int weight = Integer.parseInt(weightText.getText().toString());
Person person = new Person(name, age, height, weight);
} catch(NumberFormatException e){
e.printStackTrace();
// AND Do something here to tell your user that their input is invalid!
// don't just ignore it!
}
Currently, your person class does not have a constructor so new Person(name, age, height, weight);
would give you an error, you can create the class like this:
class Person{
private final String name;
private final int age, height, weight;
public Person(String name, int age, int height, int weight){
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
}
One thing to note is the use of final. This is not required, and is a concept that you don't need to know right now, but is very useful as your programming knowledge grows. Basically, if something is final, it's value can only be initialized once, and cannot be changed after that.
Depending on what you want to do with the Person
class, you may not want it to be final. Also note the use of private
. This is a good practice in Java and in your case, to make the fields actually useful, you'll have to use getters and setters. Or (not recommended) make the fields public
or keep them without a modifier so any class in the same package can view it and modify it if needed.
Upvotes: 0
Reputation: 483
Use a parameterized constructor and pass it into the object during initialization as such
public class Person {
public Person(String name, int age, int height, int weight) {
this.name = name;
this.age = age;
this.height = height;
....
}
String name;
int age;
int height;
int weight;
}
Person person = new Person(editText.getText().toString() , ... );
Upvotes: 1