返回位置第一个位置两个字符串是不同的

时间:2014-02-10 04:29:42

标签: java string loops passwords acm

我无法弄清楚我应该如何返回第一个位置组织和检查是不同的。我必须使用子串吗?

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");
        //???
      }
   }
  }
}

4 个答案:

答案 0 :(得分:1)

这是最简单明了的方式

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;
}

答案 1 :(得分:0)

我的解决方案是:

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;
    }

如果字符串实际上相等(包括两者都是-1),则返回null,这通常不应该是实际使用的情况,因为字符串已经知道不同。

答案 2 :(得分:-1)

如果您想检查它们是否相等,可以使用:

int val = org.compareTo(check);

如果它们相等则返回0,如果org在检查之前则返回负值,否则如果org在检查后则返回正值。

如果你真的想要返回不平等的第一个位置,请使用此功能:

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



}

编辑:在您的代码中,请按以下方式致电:

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);

      }
   }
  }
}

答案 3 :(得分:-1)

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);
    }
 }
}