• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking if a value has a certain integer in it  RSS feed

 
Steve Alpine
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
algorithm gets a little harder when the range is 1 - 10000
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach is to use regular expressions. Regex Tutorial
 
Steve Alpine
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!