-1

This code is to compare 2 strings entered by the user and it's supposed to output whether the first string is > < or = the second string.

It's working for the most part except when I enter two phrases like gh and hi, it thinks gh is greater than hi. Maybe it's looking at the size of the actual letter.

package com.mycompany._3;
import java.util.Scanner;
public class App {

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter first phrase(s): ");
        String one = scanner.next();
        
        System.out.print("Enter Second phrase(s): ");
        String two = scanner.next();
        
        int length = one.compareTo(two);
        
        if(length > 0){
            System.out.print("String one is less than string two.");
        }else if (length < 0) 
            System.out.print("String one is greater than string two.");
        else
            System.out.print("Both phrases are equal length");  
    }
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Anonymous
  • 33
  • 4

1 Answers1

2

The compareTo() method compares two strings lexicographically.

The comparison is based on the Unicode value of each character in the strings.

You need to call length() method for each string to compare lengths

if(one.length() > two.length()) ...
esmin
  • 491
  • 7
  • 14
  • This is good advice but the requirement of the assignment is that I use compareTo – Anonymous Nov 21 '22 at 22:29
  • Then your assignment is basically impossible. `compareTo` tells you nothing about length. – takendarkk Nov 21 '22 at 22:31
  • Step one is to reread the assignment and make sure they want them compared only by length - maybe length first, with lexicographic comparison as a tiebreaker? If it genuinely insists you compare length by `compareTo`, make an `Integer` out of the length and call `compareTo` on that, then look for a different professor. :) – Edward Peters Nov 21 '22 at 22:31
  • For example: "A".compareTo("B") is -1, while "A".length() = "B".length() – Christoph Dahlen Nov 21 '22 at 22:32
  • 14.3 (Comparing Strings) Write an application that uses String method compareTo to compare two strings input by the user. Output whether the first string is less than, equal to or greater than the second. – Anonymous Nov 21 '22 at 22:33
  • 1
    That doesn't say anything about length - they want lexicographic comparison. – Edward Peters Nov 21 '22 at 22:34
  • Depends on what is considered „less than“. „a“ is considered less than „b“ with respect or their alphabetical order, though both have a length of 1 – Christoph Dahlen Nov 21 '22 at 22:35
  • So both `>` and `compareTo` will go lexicographically, and I think that's by far the more common assumption on what less than or greater than mean when it comes to strings. Prof probably should have clarified, but it's a very safe assumption this means lexicographic comparison. – Edward Peters Nov 21 '22 at 22:36
  • Thanks guys for all your help. I'm still learning Java so I just needed help to figure out that small part. The code does work but I actually mistyped and that's how I found the bug where the size of the letter itself matters too. – Anonymous Nov 21 '22 at 22:42