0

I have just learned 1D Array in Java, and I wrote a code to take input from the user for a specific array length, and then print the users entered data. After that user writes a number to check if the number is present or not in the array.

I have the desired output, but in one place I couldn't understand the logic. Please guide me.

Here is the program:

    //Program to get value from user using array and print all the values

import java.util.Scanner;
class Array{
public static void main(String args[]){

int[] a = new int[5];
System.out.println("Enter 5 Numbers to Store in a Array");
for(int i = 0; i<a.length; i++){
Scanner in = new Scanner(System.in);
a[i] = in.nextInt();
}

System.out.println("Numbers you Entered are:\n");
for(int i = 0; i<a.length; i++){
System.out.println(a[i]);
}

System.out.println("Check if the number is present in the array or not:");
Scanner ing = new Scanner(System.in);
int input = ing.nextInt();

**if(input == a[4]**){
System.out.println("Number is found");
}
else{
System.out.println("Not Found");
}

}
}

and the output:

    Enter 5 Numbers to Store in an Array
56
45
78
79
80
Numbers you Entered are:

56
45
78
79
80
Check if the number is present in the array or not:
80
Number is found

I want to know why the number is found in the array size is a[4], because I have the array size a[5].

5
  • 1
    An array start at index 0 si if you have an array of length 5 the indexes are 0,1,2,3,4. You want check if a number is in the array but you check just one index is it normal ? Commented Feb 24, 2020 at 6:37
  • @OmarAldakar sorry I have just realized my logic was wrong. Thanks for answering. a[4] means it is only printing the 5th position. And I have entered 80 which is in 5th pos. Commented Feb 24, 2020 at 7:03
  • @OmarAldakar can I use for loop to traverse each element and then print the required number? Like this: for(int i = 0; i<a.length; i++){ if(input == a[i]){ System.out.println("Number is found"); } else{ System.out.println("Not Found"); } } } Commented Feb 24, 2020 at 7:06
  • Yes but not like this, you need to store in a boolean to know if you have seen or not the number at the end of the loop. Commented Feb 24, 2020 at 7:21
  • For example : boolean x = false; for(...){if(...){x = true}} then at the end of the loop you do if(x) {print('yes my number')} else print('no number ...'). Otherwise you will print not found for each numbers of the list that is not your input Commented Feb 24, 2020 at 7:23

1 Answer 1

2

Since arrays are Zero based indexing,so the position which you have found for value 80 is 4

Please refer this [https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm]

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

2 Comments

I have just realized my wrong logic. Thank you for answering
Okay,If its helped you , please upvote or accept it

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.