-1

enter image description here

?

can't get to compare string I'm entering from keyboard to array of string I must and only must declare the string I want like String c = "abc" to check if its in the array I have or not like I cant scan that abc from keyboard then try to check if its in the array or not

import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Funn {
    public static void main(String[] args)throws Exception {
        int x,q = 0,w=0,i1,j1;
        String [][] a= {{"c"},{"gfg"},{"aa"},{"q"}};
        String b = null;
        Scanner input=new Scanner(System.in);
        String c= "aa";
        System.out.println(c);

        for (int i = 0 ; i < a.length; i++)
            for(int j = 0 ; j < a[i].length; j++)
            {
                 if ( a[i][j] == c)
                 {
                      i1=i;
                      j1=j;
                      System.out.println(i1);
                      System.out.println(j1);
                      break;
                 }
            }
    }}
Eslam
  • 3
  • 2
  • 1
    please post your code here not image. – parlad Apr 03 '20 at 02:41
  • 1
    [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/q/285551/5221149) – Andreas Apr 03 '20 at 03:06
  • *"can't get to compare string I'm **entering from keyboard**"* Your code is not taking any input from keyboard, so it can't obviously compare anything you enter there. – Andreas Apr 03 '20 at 03:06
  • See also: [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Apr 03 '20 at 03:08

1 Answers1

0

Maybe you should use boolean equals(Object) that compares their contents instead of using == that checks whether they're exactly the same Object.

Since String c and "aa" in the Array are both referring to the same object in the constant pool, they are the same to the operator ==.

I wrote a simple program that may help you understand:

    public static void main(String[] args)
    {
        Scanner scanner=new Scanner(System.in);
        String input=scanner.next();//Please input "aa"
        String a="aa",c="aa";
        String b=new String("aa");//you can replace "aa" with a or c if you like
        System.out.println(a==c);//true
        System.out.println(a==b);//false
        System.out.println(a==input);//false
        System.out.println(a.equals(c));//true
        System.out.println(a.equals(b));//true
        System.out.println(a.equals(input));//true
    }
Antxl
  • 53
  • 1
  • 6