fork download
  1. import java.util.Scanner;
  2.  
  3. class StringCompare
  4. {
  5. public static void main( String args[] )
  6. {
  7.  
  8. // create Scanner to obtain input from command window
  9. Scanner scan = new Scanner(System.in);
  10.  
  11. System.out.println( "Is the first String less than, equal to or greater than the second\n\n" );
  12.  
  13. System.out.println("Enter your first string");
  14. String s1 = scan.next();
  15.  
  16. System.out.println("Enter your second string");
  17. String s2 = scan.next();
  18.  
  19. System.out.printf( "s1 == s is " + (s1 == s2));
  20. System.out.println( "s1.equals(s2) is " + (s1.equals(s2)));
  21.  
  22.  
  23. // test for equality
  24. if ( s1.equals( s2 )) // true
  25. System.out.println( "string1 and string2 are equal" );
  26. else
  27. System.out.println( "string1 and string2 are not equal" );
  28.  
  29. // test for equality
  30. if (s1.equals( s2 )) // returns boolean value, true if the two match
  31. System.out.println( "string1 and string2 are equal" );
  32. else
  33. System.out.println( "string1 and string2 are not equal" );
  34.  
  35.  
  36. // test compareTo
  37. System.out.printf(
  38. "\ns1.compareTo( s2 ) is %d", s1.compareTo( s2 ));
  39. System.out.printf(
  40. "\ns2.compareTo( s1 ) is %d", s2.compareTo( s1 ));
  41. System.out.printf(
  42. "\ns1.compareTo( s1 ) is %d\n\n", s1.compareTo( s1 ));
  43.  
  44. } // end main
  45. } // end class StringCompare
  46.  
Runtime error #stdin #stdout #stderr 0.06s 4386816KB
stdin
Standard input is empty
stdout
Is the first String less than, equal to or greater than the second


Enter your first string
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1371)
	at StringCompare.main(Main.java:14)