0

I am trying to get this program to return the index value of the number that is input by the user. It is currently returning whatever number the user inputs.

import java.lang.Math;
import java.util.Scanner;

class Arrays {
public static void main (String [] args) { 
int A[]= {3, 1, 4, 5, 9, 2, 6, 8, 7, 0};
int Result;

System.out.print("Please enter a number from 1 to 9");


Scanner stdin = new Scanner (System.in);
int x = stdin.nextInt(); 

Result = x;

if(Result == -1)
   System.out.println("\nThe number entered is not found in the array:\n"); 

else
   System.out.println("\n The index of the array element entered is: " 
   + Result);
} 

static int find (int A[], int N) {
    for(int index = 0; index < A.length; index++) 
      if(A [index] == N )
      return index;

return -1;
} 
}
2
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by LEGACYONE_2006
0
import java.lang.Math;
import java.util.Scanner;


class Arrays {
    
public static void main (String [] args) { 
int A[]= {3, 1, 4, 5, 9, 2, 6, 8, 7, 0};
int Result;

System.out.print("Please enter a number from 1 to 9");


Scanner stdin = new Scanner (System.in);
int x = stdin.nextInt(); 

Result = x;

if(find(A,Result)!=-1)
{
  System.out.println("Was found in this position");
  System.out.print(find(A,Result));
  
}
else
    System.out.println("Number not in array");
 
} 

static int find (int A[], int N) {
    for(int index = 0; index < A.length; index++) 
      if(A [index] == N )
      {
        return index; 
      }

    return -1;
} 
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.