Search code examples
javaarraysstringcomparestring-comparison

How do you do a compare of more than or less than with string


Hey guys I want to do some comparison with string by string but I don't know-how.

Like if the string of the user input is the same as the array then it will do something or not it will say not an array.

Does anybody have any suggestions to compare string? but is it possible to compare string? like the code below

Scanner input = new Scanner(System.in);
String Array[] = {"Henry", "Alex"};

System.out.print("Enter a name: ");
String ans = input.nextLine();
 
if (ans > array){
  System.out.println("Name is available");
} else {
  System.out.println("Name is not available"
}

Solution

  • To see if a String is in an Array try

        Scanner input = new Scanner(System.in);
        List<String> list = Arrays.asList("Henry", "Alex");
    
        System.out.print("Enter a name: ");
        String ans = input.nextLine();
         
        if (list.contains(ans)){
          System.out.println("Name is in list");
        } else {
          System.out.println("Name is not in list");
        }