0

I am supposed to create a program where a number is inputted, and then using a loop figure out how many number 3's there are in the number. I decided to convert my int to String, and then use charAt() since it would make life a little bit easier. When I try to run the program it doesn't even try to start the scanner and instantly gives me an IndexOutofBounds error.

import java.util.*;

public class countInt {
    static int N;
    static int c;

    public countInt() {
        Scanner kn = new Scanner(System.in);
        System.out.println("Please type in a number between 10 - 1000");
        N = kn.nextInt();
    }

    public static void main(String[] args) {
        String New = Integer.toString(N);
        int NewLength = New.length();

        for (int i=0; i <= NewLength; i++){
            if (New.charAt(i) == '3'){
                c++;
            }
        }

        System.out.println("The number contains " + c + "number 3's");
    }
}

I'm a beginner programmer, so maybe there is something stupid that I missed.

sophierrr
  • 1
  • 1

0 Answers0