1

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 ?

3 Answers 3

1

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() , ... );

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

Thank you, it works. One more thing, if i declare the class final and created a constructor, does that allow the user to create many persons ?
@Mark.222 declaring the class as final does something different than declaring the variables as final. Either way, you can create as many Persons as you want. Declaring the class as final makes it so you cannot override it. If you don't know how inheritance works yet, you will learn that later along with other OOP concepts.
Let us say if the user create three persons using the code above, how can i display all persons that have been created? "
@Mark.222 Yes, first you would probably want to use a List to store people previously created. You can Google how to use Lists in Java and go from there.
0

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

  1. 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());

  2. constructor injection

    Person person = new Person(NameeditText.getText().toString(),AgeeditText.getText().toString(), ...);

....etc

Happy Coding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.