Computer Applications
Two strings, city1 and city2, are compared using city1.compareTo(city2), and the result is less than zero. What does this indicate?
Java String Handling
3 Likes
Answer
city1 appears before city2 in an alphabetical list.
Reason — The method compareTo(String s) in Java compares two strings lexicographically. It returns:
- A negative value if the calling string (
city1) comes before the argument string (city2) in alphabetical order. - 0 if the two strings are equal.
- A positive value if the calling string (
city1) comes after the argument string (city2) in alphabetical order.
If the result is less than zero, it indicates that city1 appears before city2 lexicographically (alphabetically).
Answered By
3 Likes
Related Questions
The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)