-14

I have tried with the below code. Please help me to get the output. I want simple code that I can understand.

Suppose that number is 40:

public class PositionofElementinArray {
    public static void main(String args[]) {
    int arr[] = {10, 20, 30, 40, 50};
    int value = 40;
    for(int i=0; i < arr.length; i++)
        if(arr[i]==value){
            System.out.print(arr[i]);            
    }
        else{

        System.out.print("no element found");
    }
 }
}
River
  • 8,585
  • 14
  • 54
  • 67
  • You can't have the `else` inside the loop. Otherwise it will print "no element found" for every element that is not the target. You should `return` inside the `if` clause after printing the value to stop the loop, and print "no element found" after the loop finishes. – Salem Jun 30 '17 at 18:02
  • 6
    You're missing an opening brace for the `for` loop. – victor Jun 30 '17 at 18:02
  • Please be sure to read compiler errors carefully. Once your code compiles, be sure to step through it with a debugger to make sure that each line is doing what you expect it to. Please see [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – EJoshuaS - Stand with Ukraine Jun 30 '17 at 18:04
  • import java.util.Arrays; public class PositionofElementinArray { public static void main (String[] args) { int arr[] = {10, 20, 30, 40, 50}; int value = 40; for(int i=0; i < arr.length; i++) { if(arr[i]==value){ System.out.print("value is "+arr[i]+" position is "+i); } } else{ Here I am getting syntax error for using else statement why.??? System.out.print("no element found"); }}} – Afsar Alam Jul 02 '17 at 04:44
  • for using else statement after for loop I am getting syntax error. I want to use else to print " No element found ". – Afsar Alam Jul 02 '17 at 04:47

2 Answers2

0

if(arr[i]==value) here you are checking the value. And i is the index/position

  • This is correct. Just checking if the value at that position in the array is equal to the value he's looking for. – notphilphil Jun 30 '17 at 18:26
-1

This code will print the value and its position.according to your example value is 40 position is 3.

class Test
{
    public static void main (String[] args) 
    {
        int arr[] = {10, 20, 30, 40, 50};
        int value = 40;
        for(int i=0; i < arr.length; i++)
        {
            if(arr[i]==value){
            System.out.print("value is"+arr[i]+" "position is"+i);            
            }      
        }
    } 
}
glee8e
  • 6,180
  • 4
  • 31
  • 51
Sainath Pawar
  • 65
  • 2
  • 3
  • 10
  • This code will print the value and its position.according to your example value is 40 position is 3. – Sainath Pawar Jun 30 '17 at 18:09
  • Please do remember you can edit your own post no matter what reputation level you have. You should add your further explainations by clicking the edit link above, instead of leaving a comment. – glee8e Jul 01 '17 at 06:11
  • import java.util.Arrays; public class PositionofElementinArray { public static void main (String[] args) { int arr[] = {10, 20, 30, 40, 50}; int value = 40; for(int i=0; i < arr.length; i++) { if(arr[i]==value){ System.out.print("value is "+arr[i]+" position is "+i); } } else{ Here I am getting syntax error for using else statement why.??? System.out.print("no element found"); }}} – Afsar Alam Jul 02 '17 at 09:27
  • For using else statement after for loop I am getting syntax error. I want to use else to print " No element found " – Afsar Alam Jul 02 '17 at 09:27
  • @Afsar Alam,You should write else part within for loop as else part should be written after if..what mistake you're doing is writing else part after for loop condition – Sainath Pawar Aug 03 '17 at 17:46