Create a Java program with a method that searches an integer array for a specified integer value (see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". public static int returnIndex(int[] haystack, int needle) {
In Java please.
Add comments too!
thank you!
![Create a Java program with a method that searches an integer array for a specified integer value (see help with starting the method
header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should
throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make
and with user input for the "needle".
public static int returnIndex(int[] haystack, int needle) {](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fa8d3ff56-1af2-434e-8740-80c67d4b592b%2Fa85a4f47-9915-4e3b-80fc-fcb401951ad0%2Fq1ptc4m_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
code-
import java.util.Scanner;
public class Main
{ public static int returnindex(int[] haystack , int needle) {
if (haystack == null) {
return -1;
}
int len = haystack.length;
int i = 0;
while (i < len) {
if (haystack[i] == needle) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] haystack = { 1,2,3,4,5,6 };
Scanner sc = new Scanner(System.in);
int needle = sc.nextInt();
if (returnindex(haystack,needle)>=0){
System.out.println(returnindex(haystack,needle));}
else{
throw new ArithmeticException("Element not found in array");
}
}
}
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)