mercredi 16 novembre 2016

How to check values in a string?

So i have a string in military time format : "1532" corresponding to 3:32pm. I'm trying to write a method to check if each digit in time string is an appropriate digit. So the first element cannot be greater than 2 or equal to 0, and so forth. Currently, my code doesn't run past the second log statement and I'm hoping you guys could help!

cheers!

String mOpen = "1532";

                       Log.d("hoursTesting","pass1, length is > 2");
                        if(mOpen.getText().length() == 4)
                        {
                            Log.d("hoursTesting","pass2, length is == 4");
                            char[] tempString = mOpen.getText().toString().toCharArray();
                            if(tempString[0] != 0 && tempString[0] < 3)
                            {
                                Log.d("hoursTesting","pass3, first index is != 0 and < 3");
                                if(tempString[0] == 1)
                                {
                                    Log.d("hoursTesting","pass4, first index is 1");
                                    if(tempString[2] <= 5)
                                    {
                                        Log.d("hoursTesting","pass5, third index is <= 5, success!");
                                    }
                                }
                                else   //tempString[0] is equal to 2
                                {
                                    Log.d("hoursTesting","pass4, first index is 2");
                                    if(tempString[1] < 4)
                                    {
                                        Log.d("hoursTesting","pass5, second index is <3");
                                        if(tempString[2] <= 5)
                                        {
                                            Log.d("hoursTesting","pass6, third index is <= 5, success!");
                                        }
                                    }
                                }

                            }

                        }

Aucun commentaire:

Enregistrer un commentaire