Home > database >  How do I exclude numbers that have 5 as a digit in the hundredths in java
How do I exclude numbers that have 5 as a digit in the hundredths in java

Time:11-13

I am fairly new to java and I am looking for a more effective solution to my problem. I have a list of numbers that goes from 1 to 10 000 and need to exclude every numbers that are multiples of 3 (easy enough) and every numbers that have 5 in the hundredths, so excluding 500-599, 1500-1599 ect. I have a solution but I think it isn't efficient. Would it be possible to break number into Strings and exclude those that have 5 as number in the hundredths so I would not have to write the same statement for 1500-1599, 2500-2599 ect

for (int ii = 500;  ii<=599; ii  ){
        if (a == ii){
            return false; }
    }

CodePudding user response:

You can divide by 100 and check if the remainder after dividing by 10 is 5.

if (i / 100 % 10 != 5){
   // ...
}

CodePudding user response:

String s = String.valueOf(a);
If(s.length>2 && s.charAt(s.length-3)=='5')
{ 
  return false;
}

If I have not messed up the syntax, this should work.

  •  Tags:  
  • java
  • Related