-2

In this Java program, I want to input values from the keyboard and check if the variable contains a certain digit.

I'm using contains method to find out if a variable contains a particular digit. There is an error when I write the code. Can someone please explain the error and how to fix it?

The code:

package nested_package;

import java.util.Scanner;

public class Age_prog {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println("a=" + a + "\n" + "b=" + b);
        if (a.contains(1)) {     // ERROR IN THIS LINE
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    No we can't. `int` is a primitive type. It doesn't have methods. And `Integer` doesn't have such a method either. Hint: turn `a` into a string and test the characters to see if they match the digit character that corresponds to `b`. – Stephen C Jul 02 '21 at 14:34
  • _"There is an error when I write the code."_, please be explicit when asking questions and include the actual error message. – Mark Rotteveel Jul 02 '21 at 15:22
  • Does this answer your question? [How to check if a number contains a certain digit?](https://stackoverflow.com/questions/42007075/how-to-check-if-a-number-contains-a-certain-digit) – djm.im Jul 02 '21 at 18:02

4 Answers4

0

String has contains method, you are trying to invoke contains on int type that is causing compilation error you can either change input type to string and check contains or get number and check if its digits has the digit you are looking for

        String s = sc.nextLine(); // e.g "120";
        s.contains("1");
sanjeevRm
  • 1,541
  • 2
  • 13
  • 24
0
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("a=" + a + "\n" + "b=" + b);
if (Integer.toString(a).contains(""+1)) { //   
       System.out.println("true");
} else {
    System.out.println("false");
}
  • Integer.toString(a) converts a to a string
  • contains takes a string value of the digit
  • if you have a digit you can prepend "" to convert a string (as I did above), or specify as a string outright as "1".
WJS
  • 36,363
  • 4
  • 24
  • 39
0

If a is an Integer or int and you need to parse each digit of the number to see if there's a certain digit you should transform the number into a String and then parse it:

    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    System.out.println("a=" + a + "\n" + "b=" + b);
    String numberToString = String.valueOf(a);

    if (numberToString.contains("1")) {

        System.out.println("true");
    } else {
        System.out.println("false");
    }

    sc.close();
Simone Lungarella
  • 301
  • 1
  • 4
  • 15
0

The reason why your compiler complains is -- int (and Integer) in Java doesn't have a method named contains(). That method belongs to the String class.

One approach is to convert your numbers to String and use the contains method.
Another approach -- you can write your contains method that gets two integers and return a boolean (I named it containsDigit to avoid confusion).

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

    int a = sc.nextInt();
    int b = sc.nextInt();
    
    System.out.println("a=" + a + "\n" + "b=" + b);
    System.out.println(containsDigit(a, 1));
}

private static boolean containsDigit(int a, int b) {
    while (a > 0) {
        if (a % 10 == b) {
            return true;
        }
        a /= 10;
    }
    return false;
}
djm.im
  • 3,295
  • 4
  • 30
  • 45