The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Hi,

I am working on below challenge

http://codingbat.com/prob/p191212

Psedo code:

1. for given number % with 10

2. if 1 return true

3. else return false

I wrote my code as below

I am not passing all tests

How to improve/modify my design, code and any other alternate approaches. please advise

I am working on below challenge

http://codingbat.com/prob/p191212

Psedo code:

1. for given number % with 10

2. if 1 return true

3. else return false

I wrote my code as below

```
public boolean hasOne(int n) {
if(n%10==1){
// if(n/10==1){
return true;
// }
}
return false;
}
```

I am not passing all tests

Expected Run

hasOne(10) → true false X

hasOne(22) → false false OK

hasOne(220) → false false OK

hasOne(212) → true false X

hasOne(1) → true true OK

hasOne(9) → false false OK

hasOne(211112) → true false X

hasOne(121121) → true true OK

hasOne(222222) → false false OK

hasOne(56156) → true false X

hasOne(56556) → false false OK

other tests

X

How to improve/modify my design, code and any other alternate approaches. please advise

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Expected Run

hasOne(10) → true false X

hasOne(22) → false false OK

hasOne(220) → false false OK

hasOne(212) → true false X

hasOne(1) → true true OK

hasOne(9) → false false OK

hasOne(211112) → true false X

hasOne(121121) → true true OK

hasOne(222222) → false false OK

hasOne(56156) → true false X

hasOne(56556) → false false OK

other tests

X

i am failing above four tests

```
public boolean hasOne(int n) {
if (n % 10 == 1) {
// if(n/10==1){
return true;
// }
} else {
if ((n / 10) % 10 == 1) {
return true;
} else {
if ((n / 100) % 10 == 1) {
return true;
}
}
}
return false;
}
```

above pased all tests. any improvements or alternate approaches?
Like 12345

If

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

```
[Loop until you have checked all the digits or found a 1]
use % to get [and check] the rightmost digit, and
[then use] / to discard the rightmost digit.
```

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialIt will fail for

This checks the tens digit of two digit numbers:

```
if(n/10==1) return true
```

This checks the tens and hundreds digits of all numbers:
```
if ((n / 10) % 10 == 1) return true
if ((n / 100) % 10 == 1) return true
```

But you never check the units digit, you don't check anything higher than the hundreds digit, and you don't use the / operator to trim rightmost digits as the challenge suggested.
```
public boolean hasOne(int n) {
if(n%10==1){
// if(n/10==1){
return true;
// }
}
else {
if( (n/10)%10==1 ){
return true;
}
else {
if( (n/100)%10==1 ){
return true;
}}
}
return false;
}
```

Expected Run

hasOne(10) → true true OK

hasOne(22) → false false OK

hasOne(220) → false false OK

hasOne(212) → true true OK

hasOne(1) → true true OK

hasOne(9) → false false OK

hasOne(211112) → true true OK

hasOne(121121) → true true OK

hasOne(222222) → false false OK

hasOne(56156) → true true OK

hasOne(56556) → false false OK

other tests

OK

my code passes all test cases in codingbat but looks like it fails other test cases mentioned by you. let me refine my code further

```
package com.solution;
public class HasOne {
public static void main(String[] args) {
System.out.println("value is-->"+hasOne(21));
}
public static boolean hasOne(int n) {
if(n%10==1){
// if(n/10==1){
return true;
// }
}
else {
if( (n/10)%10==1 ){
return true;
}
else {
if( (n/100)%10==1 ){
return true;
}}
}
return false;
}
}
[code]
```

[/code]my code passed for 21 as well

```
package com.solution;
public class HasOne {
public static void main(String[] args) {
System.out.println("value is-->"+hasOne(1));
}
public static boolean hasOne(int n) {
if(n%10==1){
// if(n/10==1){
return true;
// }
}
else {
if( (n/10)%10==1 ){
return true;
}
else {
if( (n/100)%10==1 ){
return true;
}}
}
return false;
}
}
```

value is-->true

```
[code]package com.solution;
public class HasOne {
public static void main(String[] args) {
System.out.println("value is-->"+hasOne(1001));
}
public static boolean hasOne(int n) {
if(n%10==1){
// if(n/10==1){
return true;
// }
}
else {
if( (n/10)%10==1 ){
return true;
}
else {
if( (n/100)%10==1 ){
return true;
}}
}
return false;
}
}
```

[/code]value is-->true

How do you print out/find only the first/ones digit?

How do you print out/find only the second/tens digit?

How to print out the n digits that make up a number one at a time using the

How do you extend your approach to numbers with arbitrary numbers of digits?

How will you test all digits of a n-digit number like 1234567?

What is you control structure?

You can't do this without a

Stop trying to fix your code. It is a mess. Go back and think about the answers to the questions I have asked.

The if statement is not the only looping control flow statement.I meant to post;

The for statement is not the only looping control flow statement.

There is also the while statement. It will come in handy for this challenge.

above gives false instead of true(which is wrong)

```
package com.solution;
public class HasOne {
public static void main(String[] args) {
System.out.println("value is-->" + hasOne(10000));
}
public static boolean hasOne(int n) {
if (n % 10 == 1) {
// if(n/10==1){
return true;
// }
}
else {
if ((n / 10) % 10 == 1) {
return true;
} else {
if ((n / 100) % 10 == 1) {
return true;
}
}
}
return false;
}
}
```

value is-->false

let me refine my code

There is also the while statement. It will come in handy for this challenge.

below passed all tests including 10000

```
public boolean hasOne(int n) {
boolean result=false;
while (n > 0) {
if ((n % 10) == 1)
result=true;
n=(n/10);
}
return result;
}
```

any improvements or alternate approach for this?

cannot i use for loop for this which i am more habituated with?

1. loop though given array using while loop until given number greater than zero

2. if reminder of given number modulus 10 is 1 return true

3. else go to next digit by doing / on given number and again check % to see is 1

4. loop till end.

5. return false if no one found as any digiy

The number of digits in an integer

You could also assume that

Both of these techniques are less elegant and efficient than the

```
package com.solution;
public class HasOne {
public static void main(String[] args) {
System.out.println("value is-->" + hasOne(10000));
}
public static boolean hasOne(
int n) {/*
*
* while (n > 0) { if (n % 10 == 1) return true; n=n/10;
*
* } return false;
*/
// }
for (int i = n; i>0; i++) {
if (n % 10 == 1)
return true;
n = n / 10;
return false;
}
return false;
}
}
```

above give wrong resultvalue is-->false

```
package com.solution;
public class HasOne {
public static void main(String[] args) {
System.out.println("value is-->" + hasOne(10000));
}
public static boolean hasOne(
int n) {/*
*
* while (n > 0) { if (n % 10 == 1) return true; n=n/10;
*
* } return false;
*/
// }
for (int i = n; i>0; i--) {
if (n % 10 == 1)
return true;
n = n / 10;
return false;
}
return false;
}
}
```

value is-->falseeven i-- also give false as above

```
for ( [initialize expression] ; [conditional expression] ; [increment expression] ) {
[do some work]
}
```

is exactly equivalent to this code:

```
[initialize expression]
while ( [conditional expression] ) {
[do some work]
[increment expression]
}
```

In this case, you have no "initialize expression," your "conditional expression" is

How do you know when you have tested the last digit?

How many digits have you tested when you execute line 23?

Presumably that is why you are doing these challenges: to learn useful things that you don't already know.

```
package com.solution;
public class HasOne {
public static void main(String[] args) {
System.out.println("value is-->" + hasOne(10000));
}
public static boolean hasOne(
int n) {/*
*
* while (n > 0) { if (n % 10 == 1) return true; n=n/10;
*
* } return false;
*/
// }
for (int i=n; i>0;) {
if (n % 10 == 1)
return true;
n = n / 10;
}
return false;
}
}
/*In general, this for loop:
for ( [initialize expression] ; [conditional expression] ; [increment expression] ) {
[do some work]
}
is exactly equivalent to this code:
[initialize expression]
while ( [conditional expression] ) {
[do some work]
[increment expression]
}
In this case, you have no "initialize expression," your "conditional expression" is n > 0, and your "increment expression" is n = n / 10.*/
```

above passed one test

```
public boolean hasOne(int n) {
for (int i=n; i>0;) {
if (n % 10 == 1)
return true;
n = n / 10;
}
return false;
}
```

fails some testsExpected Run

hasOne(10) → true true OK

hasOne(22) → false Timed out X

hasOne(220) → false Timed out X

hasOne(212) → true Timed out X

hasOne(1) → true Timed out X

hasOne(9) → false Timed out X

hasOne(211112) → true Timed out X

hasOne(121121) → true Timed out X

hasOne(222222) → false Timed out X

hasOne(56156) → true Timed out X

hasOne(56556) → false Timed out X

other tests

X

```
package com.solution;
public class HasOne {
public static void main(String[] args) {
System.out.println("value is-->" + hasOne(10000));
}
public static boolean hasOne(
int n) {/*
*
* while (n > 0) { if (n % 10 == 1) return true; n=n/10;
*
* } return false;
*/
// }
for (int n=n; n>0;) {
if (n % 10 == 1)
return true;
n = n / 10;
}
return false;
}
}
/*In general, this for loop:
for ( [initialize expression] ; [conditional expression] ; [increment expression] ) {
[do some work]
}
is exactly equivalent to this code:
[initialize expression]
while ( [conditional expression] ) {
[do some work]
[increment expression]
}
In this case, you have no "initialize expression," your "conditional expression" is n > 0, and your "increment expression" is n = n / 10.*/
```

how to intialize in this case of for loop as while loop do not have intialization?eclipse gives below error

Multiple markers at this line

- Duplicate local variable n

- The assignment to variable n has

The reason you are having trouble with the for loop is because there is no need or reason to use

There is definitely no reason to set it to

One reasonable choice to set it to the number of digits in

although I think we were all aware of the possibility

Yes, even CodingBat itself.

http://codingbat.com/doc/java-for-while-loops.html

Here are quotes from that page.

The for-loop is a variant of the while-loop,

The for-loop can actually be understood as equivalent to a while-loop version

```
public boolean hasOne(int n) {/*
*
* while (n > 0) { if (n % 10 == 1) return true; n=n/10;
*
* } return false;
*/
// }
for (; n>0;) {
if (n % 10 == 1)
return true;
n = n / 10;
}
return false;
}
```

here is the for solution which passes all tests. Any improvements to it or alternative solutions to it?

```
public boolean hasOne(int n) {
for(;n > 0; n = n/10){
if(n % 10 == 1)return true;
}
return false;
}
```

Alternatively, we could use String methods.
```
public boolean hasOne(int n) {
String inputString = Integer.toString(n);
return inputString.contains("1");
}
```

Just for fun, I wrote a solution using recursion.
```
public boolean hasOne(int n) {
if(n <= 0)return false;
if(n % 10 == 1)return true;
return hasOne(n/10);
}
```

```
public boolean hasOne(int n) {
if(n <= 0)return false;
if(n % 10 == 1)return true;
return hasOne(n/10);
}
```

using above recursion approach below lineif(n % 10 == 1)return true;

returns true and method wil be terminated right?

After that how flow comes to below line to call same method again?

return hasOne(n/10);

Please advise

returns true and method wil be terminated right?Yes.

After that how flow comes to below line to call same method again?It doesn't when input is 321.

If we put debug statements, then we can see flow.

```
public class HasOne{
public static void main(String[] args){
System.out.println(" first test ");
System.out.println(" input 321 gives output of " + hasOne(321));
System.out.println(" second test ");
System.out.println(" input 1234 gives output of " + hasOne(1234));
System.out.println(" third test ");
System.out.println(" input 789165432 gives output of " + hasOne(789165432));
}
public static boolean hasOne(int n) {
System.out.println(" input to hasOne method was " + n);
if(n <= 0)return false;
if(n % 10 == 1)return true;
return hasOne(n/10);
}
}
```

The output is:first test

input to hasOne method was 321

input 321 gives output of true

second test

input to hasOne method was 1234

input to hasOne method was 123

input to hasOne method was 12

input to hasOne method was 1

input 1234 gives output of true

third test

input to hasOne method was 789165432

input to hasOne method was 78916543

input to hasOne method was 7891654

input to hasOne method was 789165

input to hasOne method was 78916

input to hasOne method was 7891

input 789165432 gives output of true

```
public boolean hasOne(int n) {
while (n > 0) {
if (n % 10 == 1)
return true;
n=n/10;
}
return false;
}
```

above is technically equivalent to below right

```
public boolean hasOne(int n) {
while (n > 0) {
if (n % 10 == 1){
return true;
}
n=n/10;
}
return false;
}
```

i meant to say if(condition) statement without begin { and end } will implicitly cover only next line as its statement right like above not like belowpublic boolean hasOne(int n) {

while (n > 0) {

if (n % 10 == 1){

return true;

n=n/10;

}

}

return false;

}

```
public boolean hasOne(int n) {
while (n > 0) {
if (n % 10 == 1){
return true;
n=n/10;
}
}
return false;
}
```

above give error saying

Compile problems:

Error: n=n/10;

^^^^^^

Unreachable code

i meant to say if(condition) statement without begin { and end } will implicitly cover only next line as its statement rightYes.

Unreachable codeYou just have to make the compiler happy.

Why not? do it like you did at

https://www.experts-exchange.com/questions/28966679/hasOne-challenge.html#a41778800

Java

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.