-1

Why do I get nothing when run the code?

Scanner sc = new Scanner(System.in);

    String fname = sc.nextLine();
    String[] farray = new String[]{"Chen", "Chang"};

    if(Arrays.asList(farray).contains(farray)){
        System.out.println("Exits");
    }

Is there a logical error I made?

Chen
  • 53
  • 7

2 Answers2

2

You have a typo, you used farray instead fname

if(Arrays.asList(farray).contains(fname)) {
    System.out.println("Exits");
}
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35
1

This expression will return false as it farray does not have itself in this. It is that simple as that. So nothing got printed.

Arrays.asList(farray).contains(farray) // false

You can look for element in array. like

Arrays.asList(farray).contains("Chang") // return true
TechnoTech
  • 744
  • 4
  • 14