2

When the user types a value it checks if it exists in an array.

import java.util.Scanner;

public class array1 {
    public static void main(String[]args){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a value");
        int num = scan.nextInt();
        int [] arraynumbers = {1,2,3,4,5,6,7,8,9,10};
        for(int i = 0; i < arraynumbers.length; i++) {
            if (arraynumbers[i] == num){
                System.out.println("The value you have entered " + num + ", exists in the array");
        
            }else{
                System.out.println("The value you have entered does not exist in the array");
            }
        }
    }
}

So, when ever I type a number to test it prints:

Enter a value
3
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered 3, exists in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array
The value you have entered does not exist in the array

I am not 100% sure why this happens.

Questions

  1. Is it because there is nothing stopping it from finishing when it finds a number in the array?
  2. Is there a way to prevent this?

Thank you

2
  • Enter a value 3 The value you have entered does not exist in the array The value you have entered does not exist in the array The value you have entered 3, exists in the array Commented Feb 26, 2016 at 4:20
  • please mark an answer to the question or update the question Commented Sep 16, 2017 at 12:11

3 Answers 3

3

You are probably looking out for a break. The entire loop is traversed even if your num is found. And either of the if or else block is executed. This would help you :

if (arraynumbers[i] == num) {
    System.out.println("The value you have entered " + num + ", exists in the array");
    break;
}

and probably to avoid printing anything in case the value is not matched you can remove the else block from your code.

Sign up to request clarification or add additional context in comments.

9 Comments

This will still print all the values until you hit the one you've entered.
it would execute the else unless the value is found..which is what the OP is looking for I believe.
from the Q : "nothing stopping it from finishing when it finds a number in the array"
Hey, i tried adding this but know it says this: Enter a value 3 The value you have entered does not exist in the array The value you have entered does not exist in the array The value you have entered 3, exists in the array
@JackRaiden : ideally thats what we could figure out from your question the intent was? or could you specify the required output clearly in the Question please
|
2

The break statement is definitely key. However, if you want to print whether the number is found or not, you may want to consider something like this:

int num = scan.nextInt();
int [] arraynumbers = {1,2,3,4,5,6,7,8,9,10};
String srchStr = "does not exist";

for(int i = 0; i < arraynumbers.length; i++) {
    if (arraynumbers[i] == num) {
        srchStr = "exists";
        break;
    }
}

System.out.println("The value you have entered " + num + ", " + srchStr + " in the array"); 

Comments

0

When you put that check in a loop like this, it means you check every number in the array:

for(int i = 0; i < arraynumbers.length; i++) {
}

You could do this:

List<Integer> values = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

if (values.contains(num)) {
    System.out.println("The value you have entered " + num + ", exists in the array")
} else {
    System.out.println("The value you have entered does not exist in the array");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.