
-----------------------------------
nelsonkkyy
Thu Oct 20, 2011 2:44 pm

for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------

/**
   Adds up the values from 1 to 10000, but skipping certain ones
 */
public class Adder2
{
    public static void main(String


What kind of methods can i use to get rid of the 5 in all the values?

-----------------------------------
Tony
Thu Oct 20, 2011 3:00 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
follow an example
[code]
total = 151;
[/code]
does this value have a digit 5 in it? How did you know? Do the same thing for every number.

-----------------------------------
nelsonkkyy
Thu Oct 20, 2011 6:29 pm

Re: for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
it has a 5 in it but i dun know how to detect the 5, like by using what method?

-----------------------------------
crossley7
Thu Oct 20, 2011 6:33 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
modular functions and division.

In turing notation with the most basic example.  I don't know the specific java equivalent but my guess is that % is mod and / is div provided the 123 is stored in an integer.

123 div 100 = 1
123 div 10 mod 10 = 2
123 mod 10 = 3

-----------------------------------
Tony
Thu Oct 20, 2011 6:41 pm

Re: for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
it has a 5 in it but i dun know how to detect the 5
but
How did you know?
You looked at each digit, one at a time, to see if any of them were 5. Do that.

-----------------------------------
nelsonkkyy
Thu Oct 20, 2011 6:46 pm

Re: for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
obviously,, but i couldnt find any command to do that.

-----------------------------------
Insectoid
Thu Oct 20, 2011 7:54 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
There is no command to do that. There's a lot of things you might have to do for which there is no command. That's likely the objective of the assignment- find a way to identify each digit in a number. Big hint: You can do this on paper, with math.

-----------------------------------
Tony
Thu Oct 20, 2011 8:40 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
Hey guys, I want to build a search engine to find web pages, like Google does. What's the command in Java to do that? :lol:

-----------------------------------
Nick
Fri Oct 21, 2011 7:15 am

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
in this case you're looking at the number like a sentence, checking for a specific letter. The program is looking at it like a number. Make the program look at it like a sentence. There was this one variable type that did that... what was it?

-----------------------------------
md
Fri Oct 21, 2011 9:58 am

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
Actually, looking at it like a number turns out to be much faster and easier - plus you can use some simple logic to skip ahead to the next possible non-5 number to check. 

The trick to solving the problem is to figure out how to do it by hand. Once you can describe the solution you can trivially write the Java code (in this case, not all cases are trivial).

-----------------------------------
mirhagk
Fri Oct 21, 2011 4:38 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
depends on the library but converting the value to a string and calling an index of method on it is trivial, and the speed of the program is not important in this case.

-----------------------------------
Tony
Fri Oct 21, 2011 5:52 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
trivial solutions are rarely the point of assignments.

-----------------------------------
Velocity
Fri Feb 10, 2012 10:58 am

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
if numvalue mod 5 = 1 or 2 or 3 * .75 / 500 + 2.25 * 10 ^ -4 it will have a number with 5 in it

-----------------------------------
RandomLetters
Fri Feb 10, 2012 8:41 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
There is a command for that, the good old

(""+n).indexOf('5') >= 0

-----------------------------------
Velocity
Mon Feb 13, 2012 12:16 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
or that... lol thats easier to get and comprehend.

-----------------------------------
mirhagk
Mon Feb 13, 2012 5:07 pm

RE:for loop add 1 to 10000 but skip all the values that have 5 in it.
-----------------------------------
this thread is pretty old lol, so I don't know why either of you responded. And both methods are fine, the first is faster, which might or might not matter, and the second is easier.
