Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Java Beginner String Method compareTo() problem

$
0
0
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*/

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");*/
			
	}
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>