Killerpixler
Killerpixler

Reputation: 4050

ParsingMethod for Array of Edit Texts android

My app has 4-5 EditTexts on each activity and I have 10 or so activities. I need to parse each of these to a double. So I figured I want to create a method in which I enter an EditText Array and it returns a double array with the parsed numbers.

One of the EditTexts will always be empty so I would need that specific position in the array of Doubles that are supposed to be returned to 0.

This is what I have been fiddling around with (without success so far).

public double[] parser(EditText[] editArray) {
            EditText toBeParsed[] = null;
            double parsed[] = null;
            for (int i = 0; i < editArray.length; i++) {
                try {
                    parsed[i] = Double.parseDouble(toBeParsed[i].getText().toString());
                } catch (Exception e) {
                    parsed[i] = 0;}
            }

Ho do i need to set this up?

Next in one of my activities I have this where I call the method (from a MiscMethods.java file)

EditText inputs[] = {i1,i2,i3,i4}
Double parsed[];
for (int i = 0; i < inputs.length; i++) {
                parsed[i] = MiscMethods.parser(inputs);
            }

But get a type mismatch... Why? the method returns an array of doubles and should put them into the double parsed[] array.?

Upvotes: 1

Views: 150

Answers (1)

kungfoo
kungfoo

Reputation: 597

It seems like you are using toBeParsed array in stead of your editArray

so

parsed[i] = Double.parseDouble(toBeParsed[i].getText().toString());

should be

parsed[i] = Double.parseDouble(editArray[i].getText().toString());

Upvotes: 1

Related Questions