# Java program to find out n'th value by using array
In this section, You will learn how to find out nth value by using array in Java.
Let's try to create a simple example :
➤ Example : student.java;
import java.io.*;
import java.util.Scanner;
public class FindElement {
public static void main(String args[]) {
int[] arr = {5, 10, 15, 20, 23};
Scanner input = new Scanner(System.in);
System.out.print("Enter the nth position: ");
int num = input.nextInt();
if(num <= arr.length){
int element = arr[num-1];
System.out.println("Element is : "+element);
}
else{
System.out.print("Enter any position upto "+arr.length);
}
}
}
Output :
Enter the nth position: 10
Enter any position upto 5
---------------------------------------------------------------------
Enter the nth position: 4
Element is : 20
Comments