Im a java noob. Basically I am trying to create a program that compares two command line arguments while ignoring case and prints out the lesser of the two strings. Here is what I have so far:
public class CompareStrings
{
public static void main(String[] args)
{
String s1 = new String(args[0]);
String s2 = new String(args[1]);
if ( s1.compareToIgnoreCase(s2) > 0 )
System.out.println(s2);
else if ( s1.compareToIgnoreCase(s2) < 0 )
System.out.println(s1);
else
System.out.println("Both strings are equal.");
}
}
I keep getting the error
Error: Could not find or load main class CompareString
when I try to run it. What am I doing wrong?
We can compare String in Java on the basis of content and reference. It is used in authentication (by equals() method), sorting (by compareTo() method), reference matching (by == operator) etc.
The == operator, known as the equality operator, is used to compare two strings in Java.
In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same. The input string has to be a char array of C-style string.
Java String compareTo() Method The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).
"Error: Could not find or load main class CompareString"
Your error message says you couldn't load class "CompareString", but your code says your class name is CompareStrings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With