Hello
I am trying to compare 2 Strings in dictionary order. My program currently returns the value 25.
I know this means that 2nd String is lower Alphabetically. How do I display the value 25 in a Print
Statment? I have tried using an if statement but I got an incompatible types error
/*6. Write a program that displays 2 String values in dictionary order.
Sample output:
first String: Zebra
second String: Apple
Apple comes before Zebra in the dictionary*/
I am trying to compare 2 Strings in dictionary order. My program currently returns the value 25.
I know this means that 2nd String is lower Alphabetically. How do I display the value 25 in a Print
Statment? I have tried using an if statement but I got an incompatible types error
/*6. Write a program that displays 2 String values in dictionary order.
Sample output:
first String: Zebra
second String: Apple
Apple comes before Zebra in the dictionary*/
public class Q6
{
public static void main (String[] args)
{
String str1 = new String("Zebra");
String str2 = new String("Apple");
//String str1 ="Zebra";
//String str2 ="Apple";
System.out.println(str1);
System.out.println(str2);
System.out.println(str1.compareTo(str2)); // if 2nd String is lower displays positive 25
/*if(str1.compareTo(str2))
System.out.println("Apple comes before Zebra in the dictionary");*/
}
}