Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

Checking if a value has a certain integer in it

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
Hi, say we have an integer array of integers 1 through 100.

How would we check whether that integer has the number 5 in it, so we can find the sum of all integers 1 through 100 excluding those with 5's in them.
So for example, 5, 52, 57, 85, are not eligible integers for our sum. Is there a built in method i can use ?

Ranch Hand
Posts: 679
• Number of slices to send:
Optional 'thank-you' note:

Steve Alpine wrote:Is there a built in method i can use ?

No, but the algorithm is pretty simple, so why don't you give it a go.

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
algorithm tips:
you don't need any numbers >= 50 and numbers < 60
you don't need number 5 and any other number that has 5 at the end

Steve Alpine
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
algorithm gets a little harder when the range is 1 - 10000

Bartender
Posts: 2236
63
• Number of slices to send:
Optional 'thank-you' note:
Do you know how to split an integer into separate digits?
A hint: modulo 10 gives you the last digit.

Stuart A. Burkett
Ranch Hand
Posts: 679
• Number of slices to send:
Optional 'thank-you' note:
An alternative approach is to convert the number to a String and then check if the String contains a 5. That will work for any range of values.

Jam Rei
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Another approach is to use regular expressions. Regex Tutorial

Steve Alpine
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:

Stuart A. Burkett wrote:An alternative approach is to convert the number to a String and then check if the String contains a 5. That will work for any range of values.

I was thinking the same, then you can just check the substring values and if they == x, then put them into respective variables.

Marshal
Posts: 75644
354
• Number of slices to send:
Optional 'thank-you' note:

Steve Alpine wrote: . . . check the substring values and if they == x, then put them into respective variables.

I hope you are not using == because it does not work. You have been given three different suggestions; all can be got to work. If you are using substrings, go through the String class and its methods and you will probably find something useful to ease your task.

And welcome to the Ranch

lowercase baba
Posts: 13048
67
• Number of slices to send:
Optional 'thank-you' note:
Break the problem down into component parts. some of the things you need to do:

iterate through an array, looking at each value
given some value, determine if it has a '5' in it.
sum up a bunch of integers.

 A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me: free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter