1
package javaapplication1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class arrrrr {
    public static void main(String[] args) throws IOException {
        String n,m;

      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

       ArrayList l1=new ArrayList();
        l1.add("india");
        l1.add("chennai");
        l1.add("tamilnadu");
        l1.add("mumbai");
         n=br.readLine();
        for(int j=0;j<4;j++)
        {
            if(n==l1.get(j))
            {
                System.out.println("The array location  :"+"l1["+j+"]");
            }
        }
    }
}

I want to find a array location. When given input, the program exits automatically. If I assign the value to n, it works perfect. I want to know why the programs exits after giving input.

JJS
  • 6,431
  • 1
  • 54
  • 70
simon asir
  • 49
  • 7
  • Do not forget to add a `br.readLine()` at the end to make your program wait for some input prior to exiting. – npinti Apr 29 '16 at 12:27

1 Answers1

1

Change this:

if(n==l1.get(j))

to this:

if(n.equals(l1.get(j)))

Reason is == checks references, but equals will check if strings are equal.

Adnan Isajbegovic
  • 2,227
  • 17
  • 27