【问题标题】:Return the position first position two strings are different返回位置第一个位置两个字符串不同
【发布时间】:2014-03-07 07:19:37
【问题描述】:

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

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

【问题讨论】:

    标签: java string loops passwords acm-java-libraries


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      我的解决方案是:

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

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

      【讨论】:

      • 如果您使用“fredd”输入密码并使用“fred”确认密码,则返回相同。可能需要修复
      【解决方案3】:

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

      int val = org.compareTo(check);
      

      如果相等则返回 0,如果 org 是 before check 则返回负值,否则如果 org 是 after check 则返回正值。

      如果你真的想返回他们不相等的第一个位置,使用这个函数:

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

      【讨论】:

      • int limit = org.length()>check.length?org.length():check:length();似乎有问题
      • 试试我的编辑。我在该行中添加了括号。
      • 把它放到整个程序的上下文中会有所帮助
      • 我收到“The method firstMisMatch(java.lang.String, java.lang.String) is undefined for the type CheckPasswords”错误。我真的很感谢帮助的人。
      • 这不是内置函数。这是我为你写的一个函数:)。所以把函数放在你的类中。
      【解决方案4】:
      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);
          }
       }
      }
      

      【讨论】:

      • 不过,我不明白您为什么要识别两个密码的不同之处,您只能识别它们是相同还是不同
      • if(org.length()!=check.length()) 上好像有空指针异常错误
      • @user3291384 现在检查一下
      • 由于访问 i 超出范围,因此无法编译事件。
      • @SoftwareMonkey 感谢您的指出。代码不是在编辑器上编写的,因此是一个问题。它也是为了让提问者了解实现的想法而编写的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多