4 Replies - 502 Views - Last Post: 02 November 2017 - 09:23 AM Rate Topic: -----

#1 nihal0808  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-October 17

Could someone suggest me why I am getting error

Posted 01 November 2017 - 07:25 PM

// Program to print first and last index of the array

public class FindFirstAndLast {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FindFirstAndLast obj = new FindFirstAndLast();
		int arr[]= {1,3,4,4,4,4,5,6};
		obj.find(arr, 4);
	}
	int count=0;
	int result[];
	void find(int [] arr, int key) {
	
		for(int i=0; i<arr.length; i++) {
			
			if(arr[i]==key) {
				 result[count]= arr[i];
				count++;
				
			}
			else {
				System.out.println(arr[i]);
			}
			
		}
		for(int j=0; j<result.length; j++) {
			
		}System.out.println(result[0]+ " " + result.length);
	}

}




sample output: 2,5.

This post has been edited by ndc85430: 01 November 2017 - 10:40 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Could someone suggest me why I am getting error

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 597
  • View blog
  • Posts: 2,494
  • Joined: 13-June 14

Re: Could someone suggest me why I am getting error

Posted 01 November 2017 - 10:44 PM

If you're getting errors, you need to tell us what they are and what you think they mean. As a programmer, you'll need to get used to interpreting errors and debugging.

Also, when posting code here, please do so within code tags as you're asked. I've done it for you this time.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3449
  • View blog
  • Posts: 15,618
  • Joined: 20-September 08

Re: Could someone suggest me why I am getting error

Posted 02 November 2017 - 01:56 AM

Quote

// Program to print first and last index of the array


Wouldn't those be 0 and array.length - 1 respectively?

btw, class names in Java are nouns, not made out of verbs:
http://technojeeves....yle-conventions

This post has been edited by g00se: 02 November 2017 - 01:57 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#4 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 70
  • Joined: 26-September 16

Re: Could someone suggest me why I am getting error

Posted 02 November 2017 - 03:03 AM

I'm guessing the OP meant something like this:

REMOVED



I may be wrong, though, in which case more information is needed.

This post has been edited by g00se: 02 November 2017 - 03:54 AM
Reason for edit:: Full code removed

Was This Post Helpful? 0
  • +
  • -

#5 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 70
  • Joined: 26-September 16

Re: Could someone suggest me why I am getting error

Posted 02 November 2017 - 09:23 AM

I apologize for posting more code than I should have. I'll just say that the sample output suggests that the OP wants to find the first index and last index of a specific number in the input array (in this case 4).

As for the main error in the current code, things seem to start going downhill in line 12.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1