0

I am trying to read in multiple variables (int, double, String) from one input. However, the input has to be a specific way, i.e. the user should enter either distance in miles by entering “X miles” (either in decimals or as an integer) or time by entering “Y mins” (only as an integer). Here is an example of what I coded. However, enter a double does not work. I also need to use these values in other methods.

    public boolean hintWalkTracker() {
    // tracks whether the input for walking/running activity is correct
    String text = String.valueOf(hintEditText.getText());
    String s = text.replaceAll("\\d","");
    int i = Integer.parseInt(text.replaceAll("[\\D]", ""));
    double d = Double.parseDouble(text.replaceAll("[\\D]", ""));

    if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running"))  && s.equals(" miles")) {
        d = d * 88.9;
        return true;
    } else if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running")) && s.equals(" mins")) {
        d = i * 4.78;
        return true;
    } else if ((activityDropDown.getSelectedItem().equals("Walking") || activityDropDown.getSelectedItem().equals("Running")) && ((!s.equals(" mins")) || !s.equals(" miles"))) {
        // create a new AlertDialog Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        // set dialog's message to display
        builder.setMessage(R.string.walk_missing_message);

        // provide an OK button that simply dismisses the dialog
        builder.setPositiveButton(R.string.OK, null);

        // create AlertDialog from the AlertDialog.Builder
        AlertDialog errorDialog = builder.create();
        errorDialog.show(); // display the modal dialog
        return false;
    }
    return false;
}
3
  • So what do you want? What is your trouble? Commented Mar 25, 2016 at 16:06
  • I would like it to check and see if the user is inputting the correct int/double and String. If not, I would like to throw up an AlertDialog. For example, if the user enters "12.5 mins" as their input, it should throw up the AlertDialog because 12.5 isn't an integer. Commented Mar 25, 2016 at 16:57
  • I think @Phoenix have correct answer as per your requirement, though you can definitely improve your algorithm to know when to show the alert dialog. Wanna know how? Drop an comment. :) Commented Mar 25, 2016 at 17:39

1 Answer 1

1

You just need to use a TextWatcher on your EditText in order to check what the user is writing and to react as you want when he write what you need.

Code sample :

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub
                hintWalkTracker();
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                // TODO Auto-generated method stub
            }
        });
Sign up to request clarification or add additional context in comments.

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.