Find the thousandth integer without the digits 3, 4, and 7 I have to find the thousandth number (excluding numbers 3, 4, 7) but it's taking a long time, about 9 seconds. I'd like to find out how to improve performance.

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

public class FirstChallange {

    private static int removedNumbers;
    private static int numberQuantity;
    private static int lastNumberFound; 
    private static int cont;
    private static int pos3;
    private static int pos4;
    private static int pos7;    

    public static void main(String[] args) 
    { 

        long inicio = System.currentTimeMillis();  

        removedNumbers = 0;
        numberQuantity = 10000000;


        for (cont = 1; removedNumbers &lt;= numberQuantity; cont++) {      
            String str = new String(); 
            str = String.valueOf(cont);     

            pos3 = str.indexOf("3");
            pos4 = str.indexOf("4");
            pos7 = str.indexOf("7");

            if((pos3 == -1) &amp;&amp; (pos4 == -1) &amp;&amp; (pos7 == -1)) {
                removedNumbers++; 
                if(removedNumbers == numberQuantity){ // can not find numbers (3, 4, 7)
                    lastNumberFound = cont; 
                }
            } 
        }       

        DecimalFormat dfmt = new DecimalFormat("0");

        System.out.println(dfmt.format(lastNumberFound)); 

        long fim  = System.currentTimeMillis();   
        System.out.println(new SimpleDateFormat("ss.SSS").format(new Date(fim - inicio)));  


    }
}


Is converting numbers into string and removing them with indexOf the best mode? or is there anything better than indexOf like RabinKarp?

Expected result: 180999565 in 5/4 seconds
import java.sql.Date;



You shouldn't need anything from java.sql unless you're using a relational database. java.util.Date would be more appropriate here (or nothing at all).




public class FirstChallange {



Challenge has one a and two es.

The name is not going to be very helpful in identifying the purpose of the class in six months' time, and there's no comment to say what the source of the challenge was either.




    private static int removedNumbers;
    private static int numberQuantity;
    private static int lastNumberFound; 
    private static int cont;
    private static int pos3;
    private static int pos4;
    private static int pos7;    



Variables should usually be in the narrowest scope possible. This means that static fields should be extremely rare.




        long inicio = System.currentTimeMillis();  



It's best to be consistent in the use of language: either name all the variables in Portuguese or name them all in English.

This line has trailing whitespace. If you can configure your IDE to automatically remove this, you will reduce the number of spurious changes which muddle revision control diffs.




        DecimalFormat dfmt = new DecimalFormat("0");

        System.out.println(dfmt.format(lastNumberFound)); 



That is heavily overkill for System.out.println(lastNumberFound);




        removedNumbers = 0;
        numberQuantity = 10000000;


        for (cont = 1; removedNumbers &lt;= numberQuantity; cont++) {      
            String str = new String(); 
            str = String.valueOf(cont);     

            pos3 = str.indexOf("3");
            pos4 = str.indexOf("4");
            pos7 = str.indexOf("7");



Actually iterating over objects is rarely the best way to count them. Iteration with filtering even less so. If you spend some time thinking through the mathematics before you start writing code, there's an easy way to tackle this problem by base conversion which takes about 20 milliseconds.