Question

i can't figure out how i am supposed to return the first position org and check are different. Do i have to use substrings?

import acm.program.*;

public class CheckPasswords extends ConsoleProgram
{
  public void run()
  {  

   while(true)
   {
      String org = readLine("Enter Password: ");
      String check = readLine("Confirm Password: ");

      if(org.equals(check))
      {
        println("Password Confirmed");
      }
      else
      {
        println("Passwords do not Match");
        //???
      }
   }
  }
}
Was it helpful?

Solution 3

If you want to just check if they are equal or not , you can use:

int val = org.compareTo(check);

It will return 0 if they are equal, negative value if org is before check , else positive value if org is after check.

If you really want to return the first position where they are unequal, use this function:

int firstMismatch(String org, String check)
{
    int limit = (org.length()>check.length())?org.length():check.length();
    for(int i=0;i<limit;i++)
    {
        try{
        if(org.charAt(i)!=check.charAt(i))
        {
             return i; //If one of the strings end, that's the position they are different, otherwise there is a mismatch. Either way, just return the index of mismatch
        }
        }
        catch(Exception e)
        {
             if(org.length()!=check.length())
            {

             return(i); //Execution comes here only when length of strings is unequal
         //Exception occurs because first string is smaller than
         // the second or vice versa. Say if you use "fred" and"fredd" as org and check 
         //respectively, "fred" is smaller than "fredd" so accessing org[4] is not allowed.   
         //Hence the exception.
          }

           System.out.println("Problem encountered"); //Some other exception has occured.
          return(-2);
        }  
    }
    return(-1); //if they are equal, just return -1



}

EDIT : And in your code, call as follows:

public class CheckPasswords extends ConsoleProgram
{
  public void run()
  {  

  while(true)
  {
  String org = readLine("Enter Password: ");
  String check = readLine("Confirm Password: ");
  int mismatchPosition = firstMisMatch(org,check);  
  if(mismatchPosition==-1)
  {
    println("Password Confirmed");
  }
  else
  {
    println("Passwords do not match from position "+mismatchPosition);

      }
   }
  }
}

OTHER TIPS

This is the easiest and clearest way

private int getPositionWhereTextDiffer(String a, String b) {
    int position = 0;
    while ( b.length() > position &&
            a.length() > position &&
            a.charAt(position) == b.charAt(position)) {
        position++;
    }
    return position;
}

My solution would be:

static public int indexOfDiff(String one, String two) {
    if(one!=null && two!=null) { 
        if(one==null || two==null) { 
            return 0;            
            }
        else {
            int ln=(one.length()<two.length() ? one.length() : two.length());
            for(int xa=0; xa<ln; xa++) {
                if(one.charAt(xa)!=two.charAt(xa)) { return xa; }
                }
            if(one.length()!=two.length()) {
                return ln;
                }
            }
        }
    return -1;
    }

This returns -1 if the strings are in fact equal (including if both are null), which generally should not be the case in practical use since the string will already be know to be different.

boolean isDifferent = false;
if(org!=null && check!=null) {
if(org.length()!=check.length()) {
    System.out.println("String are diff at position, "+check.length()+1);
} else {
    int mismatchPos = 0;
    for(int i=0; i< org.length();i++) {
        if(org.charAt(i)!=check.charAt(i)) {
            isDifferent = true;
            mismatchPos = i;
            break;
        }
    }
    if(isDifferent) {
        System.out.println("String are diff at position, "+mismatchPos);
    }
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top