2

What i'm trying to do below in the code, is get the user to enter two sepearate strings i know i have used first name and last name but just ignore that. So the user enters two strings and it should print the longest string. my program does not always do this. what do i need to change to make it work?

import java.util.Scanner;

 public class Q2
  {

    public static void main(String args [])
    {
    Scanner keyboardIn = new Scanner(System.in);
    String Fname;
    String Lname;

    System.out.print("Please enter first name: ");
    Fname=keyboardIn.nextLine();

    System.out.print("Please enter last name: ");
    Lname=keyboardIn.nextLine();

  if(Fname.compareTo(Lname) < 0)
  {
     System.out.println(Lname + " Is longest ");
  }
  else if(Fname.compareTo(Lname) > 0)
  {
     System.out.println(Fname + " Is longest ");
  }
 }
}
Mark Doherty
  • 21
  • 1
  • 5
  • The `compare` method doesn't (only) compare length. Strings have a `length` method which will do what you want. – lucasvw Jul 28 '17 at 19:41
  • 1
    You'll also have to decide what is the spected result when the length of the strings are equal. – Juan Carlos Mendoza Jul 28 '17 at 19:43
  • Unrelated : read about Java naming conventions. Variable names go camelCase. – GhostCat Jul 28 '17 at 19:57
  • You can accept this answer https://stackoverflow.com/questions/45381201/program-wont-always-print-out-the-longest-string/45381283#45381283 Hope it resolved your problem. – karthikdivi Jul 29 '17 at 05:13

5 Answers5

3

You should use the length

import java.util.Scanner;

public class Q2 {

    public static void main(String args[]) {
        Scanner keyboardIn = new Scanner(System.in);
        String Fname;
        String Lname;

        System.out.print("Please enter first name: ");
        Fname = keyboardIn.nextLine();

        System.out.print("Please enter last name: ");
        Lname = keyboardIn.nextLine();

        if (Lname.length() > Fname.length()) {
            System.out.println(Lname + " Is longest ");
        } else if (Fname.length() > Lname.length()) {
            System.out.println(Fname + " Is longest ");
        } else {
            // Both are of same length
        }
    }
}
karthikdivi
  • 3,466
  • 5
  • 27
  • 46
0

this is wrong

if(Fname.compareTo(Lname) < 0)
  {
     System.out.println(Lname + " Is longest ");
  }

strings have a length and that is what you need to use in order to compare first and lastname by checking the numbers of chars that are defining it

why is this wrong

Fname.compareTo(Lname)

because the result is not considering the number of chars on the string

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You are comparing the Strings, not their lengths. Your code should be changed to:

  if(Fname.length() < LName.length())
  {
     System.out.println(Lname + " Is longest ");
  }
  else if(Fname.length() > LName.length())
  {
     System.out.println(Fname + " Is longest ");
  }

I would suggest to also add a case for equality, check if Fname.length() == LName.length()

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • Thanks everyone for your great answers. I realise now I didn't need to use compareTo all i needed to use was the .length(). Thanks for all your input. – Mark Doherty Jul 28 '17 at 19:47
  • @MarkDoherty Glad it helped. Please accept an answer to indicate your issue is solved – James Wierzba Jul 28 '17 at 19:54
0

Java compareTo() method compares string lexicographically. I think a good way would be to use the length() method. See the code below

if(Fname.length()<Lname.length())
{
   System.out.println(Lname + " Is longest ");
}
else 
{
   System.out.println(Fname + " Is longest ");
 }

I am not sure what you want to do when strings are equal length. So, please make sure you check that case.

Denis
  • 1,219
  • 1
  • 10
  • 15
0

Instead of using compareTo method, I suggest using the length method. This allows you to compare string lengths directly and more easily.

Use the following statement as an example:

if (Fname.length() > Lname.length()){
  System.out.println(Fname + " Is longest ");
}
Howard P
  • 258
  • 3
  • 19