0

Hello! Can you help me correct this code? I keep seeing these errors when I execute my program. (java:937, java:1478, java:11).

import java.util.Scanner; public class FindName { public static void main(String[] args) { String findName; int flag = 0, i = 0; try (Scanner scanner = new Scanner(System.in)) { System.out.println("Check the name: "); findName = scanner.next(); } String[] names = {"Zach", "Seb","Jacob","Carlo","Harry", "Henry", "Nico", "Enrico", "Evan" ,"Chris"}; for(i = 0; i < names.length; i++) { if(names[i] == findName) { flag = 1; System.out.println("We found"+(i + 1)); break; } else { flag = 0; System.out.println("Not here"); } } if(flag == 1) { System.out.println("We found"+(i + 1)); } else { System.out.println("Not here"); } } }

29th Nov 2022, 1:31 PM
celine marie ribaya
celine marie ribaya - avatar
1 Resposta
+ 1
No error there. But use equals method for string comparisons instead of == operator. As if(names[i].equals(findName))
29th Nov 2022, 2:23 PM
Jayakrishna 🇮🇳