I wanted to find how many times 1 number appears in provided another number. I've found a solution for finding 2-digit numbers in another number, but what I wanted to do is to find 1-digit, 2-digit, ..., n-digit numbers in provided number. I dont want to create another case in switch instruction, so my question how can I avoid doing switch to make it work. Code below:

```
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.println("Provide number:");
    int number1 = sc.nextInt();

    System.out.println("Provide number you want to find in number1:");
    int number2 = sc.nextInt();

    int counter = 0;
    int digit = 1;
    int a = 10;

    while(number2/a>0){
        digit++;
        a = a*10;
    }

    switch(digit){
        case 1:{
            while(number1 > 0 ){
                if(number1 % 10 == number2){
                    counter++;
                }
                number1 = number1/10;
            }
        }
        case 2:{
            while(number1 > 0){
                if(number1 % 100 == number2){
                    counter++;
                }
                number1 = number1/10;
            }
        }
    }
    System.out.println(counter);
}

```

Thanks for your help.
['https://Stackoverflow.com/questions/37833775', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6469057/']
You have declared `i` inside of the `for` loop:

```
for(int i=0;i<t;i++)

```

Therefore, `i`'s scope is limited to that `for` loop. `i` does not exist outside of that `for` loop.

So, when you try to reuse that `i` in the next `for` loop

```
for(i=0;i<t;i++)  

```

you get an error. You have to declare `i` again:

```
for(int i=0;i<t;i++)

```

Same thing in your last `for` loop. It would also be very helpful if you indented your code correctly.

---

```
#include<iostream>  
using namespace std;  

int main()  
{  
    int temp,t,a[10];  
    cin>>t;  
    for(int i=0;i<t;i++)  
    {  
        cin>>a[i];  
    }  
    for(int i=0;i<t;i++)  
    {  
        int j=i+1;  
        for( ;j<t;j++)  
        {  
            if(a[i]>a[j])  
            {  
                temp=a[i];  
                a[i]=a[j];  
                a[j]=temp;  
            }  
        }  
    }  
    for(int i=0;i<t;i++)  
    {  
        cout<<a[i]<<endl;  
    }  
    return 0;  
}

```
You have forgot to declare all your variables in the begining. So this way the variables are declared as a local integer and cannot be used outside the `for loop`. I tried declaring them outside the function `int main()` and it worked
The right code:

```
#include<iostream>
using namespace std;
int i = 0;
int a[10];
int temp = a[i];
int t;
int main()
{

    cin>>t;
    for(int i=0;i<t;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<t;i++)
    {
        int j=i+1;
        for( ;j<t;j++)
        {
            if(a[i]>a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    for(i=0;i<t;i++)
    {
        cout<<a[i]<<endl;
    }
    return 0;
}

```