1
import java.util.Scanner;

public class hil {
    public static void main(String[] args){
        Scanner m = new Scanner(System.in);

        System.out.print("Insert a number:");
        int num = m.nextInt();

        int[] ar = { 10, 20, 30, 40, 50 };
        int sum = 0;

        for (int i = 0; i < ar.length; i++) {
            sum += i;

            for (int n : ar) {
                if (num == n) {
                    System.out.print(true);
                } else {
                    System.out.print(false); 
                }
            }
        }   
    }
}

This my code I do get the right output but it shows true and false for all the values in the array . Not only the single value. How to get the true and false boolean value for the value entered not the rest of the value .

current output:

Insert a number:20
falsetruefalsefalsefalsefalsetruefalsefalsefalsefalsetruefalsefalsefalsefalsetruefalsefalsefalsefalsetruefalsefalsefalse

desired output:

Insert a number:20
true

candied_orange
  • 7,036
  • 2
  • 28
  • 62
shaikh95
  • 37
  • 5
  • If you're looking to see if an array contains a value (e.g. what the user entered), [that question already has an answer](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) .. but based on your code, it's not clear what the intent is; you loop `ar.length` times maintaining a `sum` variable, then have a nested loop that is checking if the user input matches the iteration .. to re-iterate what @Mureinik said .. what is the code supposed to do? – txtechhelp Dec 18 '16 at 19:49

5 Answers5

1

I believe you're looking for the dirty bit pattern.

import java.util.Scanner;

public class hil {
    public static void main(String[] args){
        Scanner m = new Scanner(System.in);

        System.out.print("Insert a number:");
        int num = m.nextInt();

        int[] ar = { 10, 20, 30, 40, 50 };

        Boolean found = false;

        for (int n : ar) {
            if (num == n) {
                found = true;
                break;
        }   

        System.out.print(found);
    }
}

The idea is to initialize the bit (boolean in this case) to be 'clean' then give it a chance to get 'dirty'. Once 'dirty' it stays 'dirty'. The pattern works regardless of whether you consider 'clean' to be true or false.

Here the logic works out to be the same as 'OR'ing all of those equality comparisons together.

Adding the break is merely a micro optimization. Logic is the same as without it. Adding it just makes it a tiny bit faster. Maybe.

candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • upvoted for the only one to mention a `break` .. a micro optimization only in this instance (since there are only 5 items in the array), but something the OP needs to be aware given they are a novice. – txtechhelp Dec 18 '16 at 19:58
  • I think one could argue a long time if a *newbie* should worry a second on micro-optimisations and using break statements in his first assignments ... – GhostCat Dec 18 '16 at 20:17
  • I call it a micro optimization because in the world of Big O notation this averages out as O(0.5n) which is still just O(n). IE it's the sort of improvement I wouldn't hesitate to remove if it made reading the code easier. – candied_orange Dec 18 '16 at 20:40
0

Assuming that you simply want to check if the number provided by the user is in that array; well you have to check for exactly that; and remember that decision:

boolean numberFound = false;
for (int numberFromArray : nums) {
  if (numberFromArray == num) {
    numberFound = true;
  }
}

And then after the loop ... print that boolean. Once.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Just store a boolean outside of the loop and then print that.

boolean inArray = False;
for(int n: ar){
    if (num == n){
        inArray = True;
    }
}
System.out.print(inArray);
Alex
  • 759
  • 5
  • 12
0
import java.util.Scanner;

public class hil {
    public static void main(String[] args) {
        Scanner m = new Scanner(System.in);

        System.out.print("Insert a number:");
        int num = m.nextInt();

        int[] ar = {10, 20, 30, 40, 50};
        int sum = 0;
        Boolean inArray = false;
        for (int n : ar) {
            sum += n;
            if (num == n) {
                inArray = true;
            }
        }
        System.out.println(inArray);
        System.out.println(sum);
    }
}

Note: I assume that you need sum of the elements also that's why you tried to use another loop.

Apurva Gupta
  • 99
  • 2
  • 9
-2

I think if you want to print print true. you should enclose it in double quotes. like : System.out.println("true");

........................i may be wrong...but i tried to suggest a possible solution.

krishankantray
  • 73
  • 1
  • 2
  • 8