1

I have this loop I am iterating through. I need to check for an int at every position except two points which will not have an int. I am trying to come up with the best way to skip those index points.

for(int i = 0;i < 11;i++) {
    if(!Character.isDigit(number.charAt(i))){
        throw new SocSecException("At index "+i+" you should have a int ");
    }
}

I was trying to wrap an extra if statement in suggesting

if(i == 3 && i == 6 i++)

Hoping to skip the 3 and 6 index but instead it was not throwing errors anywhere in the check.

How do I exclude the 3 index and 6 index from throwing an error in this statement?

Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
wuno
  • 9,547
  • 19
  • 96
  • 180

3 Answers3

3

Just put the condition in the if condition combined with and operators.

for(int i = 0;i < 11;i++) {
    if(i!=3 && i!=6 && !Character.isDigit(number.charAt(i))){
        throw new SocSecException("At index "+i+" you should have a int ");
    }
}
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
  • Thank you. Got it. – wuno Apr 15 '16 at 03:13
  • This makes an already crowded if statement, even harder to read. – Matt C Apr 15 '16 at 03:15
  • 1
    @MatthewCliatt If that's an issue for you, then you could just as easily format the code with line breaks before each `&&`. I've seen much much worse. – 4castle Apr 15 '16 at 03:16
  • @MatthewCliatt Yes, if so we can break it into an if elseif statements. But in this case, this is just a short bit of code. – Imesha Sudasingha Apr 15 '16 at 03:16
  • @4castle Why not separate the condition into its own if statement? – Matt C Apr 15 '16 at 03:17
  • @MatthewCliatt See [this](http://stackoverflow.com/questions/5259938/what-is-better-multiple-if-statements-or-one-if-with-multiple-conditions) or [this](http://stackoverflow.com/questions/1928712/how-to-split-up-complex-conditions-and-keep-short-circuit-evaluation). The reason is because of short-circuiting. – 4castle Apr 15 '16 at 03:23
  • @4castle There are advantages and disadvantages of each, but when you have 3 conditions in a loop, you may want to consider separating them out. This is especially true if two of those conditions are closely linked together, in what they test for, and if it would improve readability if they were separated. Also note that the post you linked was debating on ***nested*** `if-else statements`, and if you see my answer, you'll see that there is no need for nesting. – Matt C Apr 15 '16 at 03:27
  • @4castle According to the **GOD** of computer science, "Premature optimization is the root of all evil." See [this](http://c2.com/cgi/wiki?PrematureOptimization). There is no need to optomize this code, especially since it probably only loops through a small (less than 1000 elements) array, and ***especially*** since the poster didn't ask for it. That would mean you're doing premature optimization. – Matt C Apr 15 '16 at 03:30
  • @4castle Taking into account premature optimization, and when to avoid it, I think that readability is the right call here. – Matt C Apr 15 '16 at 03:31
1

From what I have understood, you would need an OR condition

for(int i = 0; i < 11; i++)
{
     if ( i == 3 || i == 6 )
     {
          continue;
     }
     else if(!Character.isDigit(number.charAt(i)))
     {
        throw new SocSecException("At index " + i + " you should have an int             ");
     }
}
KP.
  • 393
  • 1
  • 12
1

EDIT:

It depends. Do you still want to do other things in your for loop with the elements at index 3 and 6? If you do, then you need to include a condition inside your current if statement.

If you still want to do other things with elements 3 and 6, but you don't want them to throw an exception, then this is how you must do that:

for(int i = 0; i < 11; i++) {
    if( i != 3 &&
        i != 6 &&
        !Character.isDigit(number.charAt(i))
       ) {
            throw new SocSecException("At index " + i + " you...");
    } 
}

Otherwise, I think it looks best to add an if statement to the beginning of your loop

for(int i = 0; i < 11; i++) {
    if( i == 3 || i == 6 ) {
        continue;
    }

    if( !Character.isDigit(number.charAt(i)) ){
        throw new SocSecException("At index " + i + " you...");
    }
}

Here's why I think the separate if statement looks nicer than the multiple conditions in one if statement:

The cleanest way to skip over certain indexes, is to add in an if statement at the beginning of the loop.

If i is one of those certain indexes, continue.

continue means to skip to the next iteration of the loop.

for(int i = 0; i < 11; i++) {
    if( i == 3 || i == 6 ) {
        continue;
    }

    if( !Character.isDigit(number.charAt(i)) ){
        throw new SocSecException("At index " + i + " you...");
    }
}

This method keeps the rest of your loop clean, and makes it clear what you are doing.

You do not want to add the condition into your existing if statement. Adding it into the existing if statement makes it seem like the two conditions must be tested together, because of some unclear reason.

If you make it into two if statements, then it is clear what each if statement is responsible for. This is important to other programmers, your professors, your coworkers, and especially yourself.

Looking at your code, you want to immediately say, "Oh, this if statement is to skip over indexes 3 and 6, and oh this if statement is to throw an exception.". And with the code in this answer, you can accomplish exactly that.

Community
  • 1
  • 1
Matt C
  • 4,470
  • 5
  • 26
  • 44
  • I disagree with you. Short-circuiting isn't any less readable if you use line breaks. Besides, if you want to convey the reasoning behind something, you should be using comments instead. – 4castle Apr 15 '16 at 03:30
  • @4castle I don't see an answer here that uses line breaks. – Matt C Apr 15 '16 at 03:33