Click here to Skip to main content
13,050,450 members (78,238 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i have an array with 5 elements in it. Now, what I want is, when I enter one of those elements, the program should show me the address of that entered array.
import java.util.Scanner;
public class location_of_an_element_in_array
	public static void main(String[] args)
		int arr[];
		arr = new int[5];
		int i;
		int search;
		Scanner input = new Scanner(
		System.out.println("Enter number:");
		arr[i] = input.nextInt();
		System.out.println("\nWhich number's locattion you want to search?");
		search = input.nextInt();

This is what I've done so far. Please help on how to achieve the desired task.
Posted 27-Jan-13 6:10am
Updated 27-Jan-13 6:15am
Sergey Alexandrovich Kryukov 27-Jan-13 12:17pm
You mix app an array with its element and generally hardly understand what you are doing...
suhan 2012 27-Jan-13 12:26pm
and that's why I need your help.
Sergey Alexandrovich Kryukov 27-Jan-13 13:05pm
Yes, I understand that, but, as a result of your confusion, you did not explain what do you want to achieve. You always need to start with the ultimate goal, not technical terms. In particular, what this array should express, what's the scenario and why?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your code is quite weak:

- class names start with capital letter. Always!
- each code line has to end with a ";"
- init and assign variables as you need them - in the loop-header, when asking for a value. That limits the scope (not here in the example, but in general)
- the for-loop:
- all being beside of humans do count from 0. Do so too please.
- a loop with 5 elements is counted therefore from 0-4 (end condition "<5" is preferable over "<=4").

public class Location_of_an_element_in_array {
	public static void main(String[] args) {
		int arr[] = new int[5];
		// fill Array
		Scanner input = new Scanner(;
		for (int i = 0; i < 5; i++) {
			System.out.println("Enter number:");
			arr[i] = input.nextInt();
		// search
		System.out.println("\nWhich number's location you want to search?");
		int search = input.nextInt();
		//TODO: add loop with search here!

Now the code runs.
Please use Eclipse or Netbeans for coding - both IDE will guide you through the process! There is no reason not to use one of them.

This is a homework task. You will have to add the search yourself. Please ask when you get stuck in that, also debugging the code can help you a lot (google for that according to the IDE you're using).

Have fun!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100