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
Vrund Patel
11,994 PointsChecking 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
Seth Reece
32,867 PointsProbably 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
Vrund Patel
11,994 PointsVrund Patel
11,994 PointsProgram is to generate numbers from 1 to 210 and check which contains 4. Should I convert each to String?
Seth Reece
32,867 PointsSeth Reece
32,867 PointsYou 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).
Vrund Patel
11,994 PointsVrund Patel
11,994 PointsCannot convert int to a String - error.
Vrund Patel
11,994 PointsVrund Patel
11,994 PointsGot it to work.
Seth Reece
32,867 PointsSeth Reece
32,867 PointsNice! Good work!