Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Checking if an integer is present in a number.

For example: 144, 148, 204, 14, 4, 124

How do I check a if 4 is present in an integer and print something if it is present. I have to go through an array of numbers 1-210.

I tried

i % 10 == 4 

However that does not take care of all the numbers containing 4.

1 Answer

Probably need to put each number in a variable as a string and use contains. http://www.tutorialspoint.com/java/number_tostring.htm http://www.tutorialspoint.com/java/lang/string_contains.htm

Program is to generate numbers from 1 to 210 and check which contains 4. Should I convert each to String?

You might be able just check it in an if statement by making a String x = 4 and checking if i.toString().contains(x). that or as part of your loop Make String n = to i.toString() and then if n.contains(x).

Cannot convert int to a String - error.

Got it to work.

Nice! Good work!