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

Help! I\'m following the directions, I think for this java lab but I have no ide

ID: 3681674 • Letter: H

Question

Help! I'm following the directions, I think for this java lab but I have no idea what I'm doing. Here are the instructions (note my other code works expect for the length() and charAt() which I have NO IDEA how to code properly...I try but keep getting an else without an if):

Here is my code:

import java.util.Scanner;

public class CompareStrings
{
private static String firstString;
private static String secondString;
private static int value;
private static int count;


public static void main(String[] args)
{
   Scanner input = new Scanner(System.in);
   System.out.printf("%nPlease enter first string:");
   firstString = input.nextLine();
   System.out.printf("%nPlease enter second string:");
   secondString = input.nextLine();

   System.out.printf("%nCompare Strings using compareTo():");
   value = firstString.compareTo(secondString);
   if(value == 0)
   {
     System.out.printf("%n%s==%s", firstString, secondString);
   }//end if
   else if(value > 0)
   {
    System.out.printf("%n%s>%s", firstString, secondString);
   }//end else-if
   else if(value < 0)
   {
     System.out.printf("%n%s<%s", firstString, secondString);
   }//end else-if


   System.out.printf("%nCompare Strings using equals():");
   if(firstString.equals(secondString))
   {
    System.out.printf("%n%s and %s are lexicographically equal", firstString, secondString);
   }//end if
   else
   {
    System.out.printf("%n%s and %s are not lexicographically equal", firstString, secondString);
   }//end else


   System.out.printf("%nCompare Strings using equalsIgnoreCase():");
   if(firstString.equalsIgnoreCase(secondString))
   {
    System.out.printf("%n%s and %s are equal and case insensitive", firstString, secondString);
   }//end if
   else
   {
    System.out.printf("%n%s and %s are not equal", firstString, secondString);
   }//end else


   System.out.printf("%nCompare Strings using length() and charAt():");
   if(firstString.length() == secondString.length())
   {
   System.out.printf("%n%s and %s are of the same length. Now lets check if they are both equal.",
                     firstString, secondString);        
   count = firstString.length();

   for (int whatever = 0; whatever < count; whatever++)
   {
     if (firstString.charAt(whatever) != secondString.charAt(whatever))
     {
      System.out.printf("%n%s and %s are not equal though they are are of the same length.",
                       firstString, secondString);
      }//end IF
      else
      {
      System.out.printf("%n%s and %s are not equal though they are of same length.",
                        firstString, secondString);
      }//end else
   }//end for
    else
    {
      System.out.printf("%n%s and %s are not equal.",firstString, secondString);
    }//end else
   }//end if

}//end main

}//end compareStrings

Explanation / Answer

Hi I have corrected your implementation. Please test it.

import java.util.Scanner;

public class CompareStrings {
   private static String firstString;
   private static String secondString;
   private static int value;
   private static int count;

   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);
       System.out.printf("Please enter first string: ");
       firstString = input.nextLine();
       System.out.printf("%nPlease enter second string: ");
       secondString = input.nextLine();
       System.out.printf("%nCompare Strings using compareTo():");
      
       value = firstString.compareTo(secondString);
       if(value == 0)
       {
           System.out.printf("%n%s==%s", firstString, secondString);
       }//end if
       else if(value > 0)
       {
           System.out.printf("%n%s>%s", firstString, secondString);
       }//end else-if
       else if(value < 0)
       {
           System.out.printf("%n%s<%s", firstString, secondString);
       }//end else-if


       System.out.printf("%nCompare Strings using equals():");
       if(firstString.equals(secondString))
       {
           System.out.printf("%n%s and %s are lexicographically equal", firstString, secondString);
       }//end if
       else
       {
           System.out.printf("%n%s and %s are not lexicographically equal", firstString, secondString);
       }//end else


       System.out.printf("%nCompare Strings using equalsIgnoreCase():");
       if(firstString.equalsIgnoreCase(secondString))
       {
           System.out.printf("%n%s and %s are equal and case insensitive", firstString, secondString);
       }//end if
       else
       {
           System.out.printf("%n%s and %s are not equal", firstString, secondString);
       }//end else


       System.out.printf("%nCompare Strings using length() and charAt():");
       if(firstString.length() == secondString.length())
       {
           System.out.printf("%n%s and %s are of the same length. Now lets check if they are both equal.",
                   firstString, secondString);
           count = firstString.length();
           int whatever = 0;
           for (whatever = 0; whatever < count; whatever++)
           {
               if (firstString.charAt(whatever) != secondString.charAt(whatever))
               {
                   System.out.printf("%n%s and %s are not equal though they are are of the same length.",
                           firstString, secondString);
                   break;
               }//end IF
           }//end for
           if(whatever == count)
               System.out.printf("%n%s and %s are not equal though they are of same length.",
                   firstString, secondString);
       }// end IF
       else
           {
               System.out.printf("%n%s and %s are not equal.",firstString, secondString);
           }//end else
       }//end main

   }//end class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote