Reputation: 9875
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?
Upvotes: 1
Views: 5484
Reputation: 4555
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.
Upvotes: 1
Reputation: 393
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 ");
}
}
Upvotes: 1
Reputation: 3570
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 ");
}
}
Upvotes: 3