0

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!");
                 }
            }
        }
   }

}
2
  • Have you tried debugging? It will probably be obvious if you can visually see what is happening at each step. Commented Nov 17, 2016 at 1:14
  • do you need to support leap seconds? Commented Nov 17, 2016 at 1:22

3 Answers 3

3

tempString contains characters, not numbers. i.e. '0' not 0 etc.

Easiest fix is to compare characters e.g. tempString[0] == '1' Alternatively, you can do something like int digit1 = tempString[0] - '0'; - but that kind of assumes you already know you just have digits in the string.

Note that cos of those clever ASCII guys and their tricky character set '0' < '1' < '2' etc, so you can still say if (str[0] < '2') etc. You just need to be a bit careful that you are only dealing with digits.

Personally I'd convert the first 2 chars to a number and the second 2 chars to a number and then just check 0 <= number1 <= 23 and 0 <= number2 <= 59.

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

2 Comments

yeah i have digits in the string, thanks ill try that out.
but what about if i want to check if the char is less than equal to the value?
1

You are comparing char with int here:

if(tempString[0] != 0 && tempString[0] < 3)

It should work like this:

if(tempString[0] != '0' && tempString[0] < '3')

Comments

1

I would substring the hours and minutes components and then check to see if each one be in range:

public boolean isTimeValid(String mOpen) {
    int hours = Integer.parseInt(mOpen.substring(0, 2));
    int minutes = Integer.parseInt(mOpen.substring(2));

    if ((hours >= 0 && hours <= 24) && (minutes >= 0 && minutes <= 59)) {
        return true;
    }
    else {
        return false;
    }
}

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.