Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

String Length Comparison After part 1, write a segment of code which will determ

ID: 675311 • Letter: S

Question

String Length Comparison After part 1, write a segment of code which will determine which String has the longer length or if they are the same. For example, if the user entered "hello" and "world" in the first part, the output would be: The strings have the same length. If the user entered "Java is fun" and I like to code, then the output should be: "I like to code" is longer than "Java is fun" Below is an example of what your output should roughly look like when this lab is completed. All text in bold represents user input.

Explanation / Answer

package chegg;

import java.util.Scanner;

public class StringLength {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       String s1,s2;

      

       System.out.print("Enter a string: ");

       s1 = sc.nextLine();

       System.out.print("Enter another string: ");

       s2 = sc.nextLine();

      

       if(s1.length() > s2.length()) {

           System.out.println("""+s1+"" is longer than ""+s2+"".");

       } else if(s2.length() > s1.length()) {

           System.out.println("""+s2+"" is longer than ""+s1+"".");

       } else {

           if(s1.equals(s2)) {

               System.out.println("The strings are the same.");

           }

           System.out.println("The strings have the same length.");

       }

   }

}