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"
}
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");
}
You can iterate the array and compare the string with each element. Once a match is found, you should print the Found
message and break the loop. If a match is not found throughout the loop, print the Not found
message after the loop.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String array[] = { "Henry", "Alex" };
System.out.print("Enter a name: ");
String ans = input.nextLine();
boolean found = false;
for (String s : array) {
if (s.equals(ans)) {
System.out.println("Name is available");
found = true;
break;
}
}
if (!found) {
System.out.println("The name does not exist in the array");
}
}
}
A sample run:
Enter a name: Alex
Name is available
Another sample run:
Enter a name: Nisha
The name does not exist in the array
Note that I have used a boolean
variable, found
to track if ans
exists in the array. There is a way to do it even without using such an additional variable:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String array[] = { "Henry", "Alex" };
System.out.print("Enter a name: ");
String ans = input.nextLine();
int i;
for (i = 0; i < array.length; i++) {
if (array[i].equals(ans)) {
System.out.println("Name is available");
break;
}
}
// If the loop has been completed without `break`, it means `name` does not
// exist
if (i == array.length) {
System.out.println("The name does not exist in the array");
}
}
}
A sample run:
Enter a name: Alex
Name is available
Another sample run:
Enter a name: Nisha
The name does not exist in the array
There is not one way how to detect is value in the list. First you have to convert your array into list. And there is a methods:
List have method called contains()
which returns boolean value. True - if value/object is in the list, false - if value is not in the list.
Example:
String[] array = {"apple", "orange"};
List<String> list = Arrays.asList(array);
String value = "apple";
if (list.contains(value)) {
System.out.println("Value is in the list");
} else {
System.out.println("Value is not in the list");
}
This is the easiest way how to detect is array contains value/object. Also instead of using .contains()
you can use .indexOf()
method which returns -1 if value doesn't exist in array. It's the oldest way used in other programming languages as well: if (list.indexOf(value) != -1)
.
The problem with these methods could be when entered value and values in array have a different char cases (upper/lower). To solve that you should iterate array and use same char case.
Example:
String[] array = {"Apple", "Orange"}; String value = "apple"; boolean isInArray = false; for (String item : array) { isInArray = item.toLowerCase().equals(value.toLowerCase()); if (isInArray) { System.out.println("Value is in the list"); break; // exiting array iteration when value was found } } if (!isInArray) { // if all array was iterated and no matching value was found System.out.println("Value is not in the list"); }
Example:
String[] array = {"apple", "orange"}; List<String> list = Arrays.asList(array); String value = "apple"; boolean isInArray = list.stream().anyMatch(item -> item.toLowerCase().equals(value.toLowerCase())); if (isInArray) { System.out.println("Value is in the list"); } else { System.out.println("Value is not in the list"); }
A bit of info from w3schools :
The equals() method compares two strings, and returns true if the strings are equal, and false if not.
Tip: Use the compareTo() method to compare two strings lexicographically.
A sample snippet of code :
String myStr1 = "Hello";
String myStr2 = "Hello";
System.out.println(myStr1.compareTo(myStr2));
This returns 0 if they are equal. If not it returns some value you can run a if else by passing the returned value to a variable if it equals 0 print equal else print not equal
Hope i have answered
If you want to search and find if a string is in an array, please check this link for better info https://www.geeksforgeeks.org/check-if-a-value-is-present-in-an-array-in-java/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.