这和我之前的相关 问题

我正在解决 UVA 的编辑阶梯问题,并试图让在线法官遵守我的答案。

我已经使用 ReadLn() 方法来调整我的文本文件读取程序:

import java.io.*;
import java.util.*;

class LevenshteinParaElJuez implements Runnable{
    static String ReadLn(int maxLength){  // utility function to read from stdin,
                                          // Provided by Programming-challenges, edit for style only
        byte line[] = new byte [maxLength];
        int length = 0;
        int input = -1;
        try{
            while (length < maxLength){//Read untill maxlength
                input = System.in.read();
                if ((input < 0) || (input == '\n')) break; //or untill end of line ninput
                line [length++] += input;
            }

            if ((input < 0) && (length == 0)) return null;  // eof
            return new String(line, 0, length);
        }catch (IOException e){
            return null;
        }
    }

    public static void main(String args[]) // entry point from OS
    {
        LevenshteinParaElJuez myWork = new LevenshteinParaElJuez();  // Construct the bootloader
        myWork.run();            // execute
    }

    public void run() {
        new myStuff().run();
    }
}
class myStuff implements Runnable{
    public void run(){

        ArrayList<String> theWords = new ArrayList<String>();
        try
        {

        /// PLACE YOUR JAVA CODE HERE

        String leido=LevenshteinParaElJuez.ReadLn(100);

        //System.out.println("lo leido fue "+leido);

        while (!leido.equals(" ")){
        theWords.add(leido);
        leido=LevenshteinParaElJuez.ReadLn(100);
        }


        }catch(Exception e){
            System.out.println("El programa genero una excepcion");
        }


        int maxEdit=0;
        int actualEdit=0;

     int wordsIndex1 =0, wordsIndex2=0;


     while (wordsIndex1<= theWords.size())
     {
      while (wordsIndex2<= theWords.size()-1){
         actualEdit=Levenshtein.computeLevenshteinDistance(theWords.get(wordsIndex1),theWords.get(wordsIndex2));
         if (actualEdit>maxEdit){maxEdit=actualEdit;}
         wordsIndex2++;
      }
     wordsIndex1++;

     }

     System.out.println(maxEdit+1);



    }


}
class Levenshtein {
    private static int minimum(int a, int b, int c) {
        if(a<=b && a<=c)
            return a;
        if(b<=a && b<=c)
            return b;
        return c;
    }

    public static int computeLevenshteinDistance(String str1, String str2) {
        return computeLevenshteinDistance(str1.toCharArray(),
                                          str2.toCharArray());
    }

    private static int computeLevenshteinDistance(char [] str1, char [] str2) {
        int [][]distance = new int[str1.length+1][str2.length+1];

        for(int i=0;i<=str1.length;i++)
                distance[i][0]=i;

        for(int j=0;j<=str2.length;j++)
            distance[0][j]=j;

        for(int i=1;i<=str1.length;i++)
            for(int j=1;j<=str2.length;j++)
                distance[i][j]= minimum(distance[i-1][j]+1,
                                        distance[i][j-1]+1,
                                        distance[i-1][j-1]+
                                        ((str1[i-1]==str2[j-1])?0:1));

        return distance[str1.length][str2.length];
    }


}

我应该阅读在线法官的全部输入,因为它是通过键盘编写的,但是当我运行上面的程序时,我无法让它停止阅读。它是这样的:

abc
cba
aba
cca

无法停止控制台读取。我该如何解决这个问题?我怀疑问题出在我的循环条件中:

String leido=LevenshteinParaElJuez.ReadLn(100);

            //System.out.println("lo leido fue "+leido);

            while (!leido.equals(" ")){
            theWords.add(leido);
            leido=LevenshteinParaElJuez.ReadLn(100);
            }

我也用过:

while (!leido.equals(null)){
        theWords.add(leido);
        leido=LevenshteinParaElJuez.ReadLn(100);
        }

也被卡住了。

编辑:该声明实际上是:

while (leido != null)){
        theWords.add(leido);
        leido=LevenshteinParaElJuez.ReadLn(100);
        }

我不明白为什么它失败了。我希望在输入第一个空行时停止通过键盘读取输入。

编辑:感谢 rodion 的回答,ReadLn 方法现在更改为:

if ((input < 0) || (length == 0)) return null;  // eof

代替:

if ((input < 0) && (length == 0)) return null;  // eof

现在,它在生成整数输出之前读取两个空格。我怎样才能改变它只读一篇?

有帮助吗?

解决方案

的问题是,输入将不<0,当它到达该返回null行,所以做:

while (leido.length() != 0) {
    ....
}

其他提示

我真的不知道,如果我正确地理解您的问题。如果你只是想你的程序停止从控制台读取,你可以按Ctrl + d(Linux)或按Ctrl + Z(Windows)中“关闭”控制台。这导致System.in.read()返回-1,使得ReadLn方法将返回空值。

检查null作为纳撒尼尔建议。

修改:(根据您的评论)

问题是与在ReadLn方法这一条件

if ((input < 0) && (length == 0)) return null;

如果你输入一个空行,长度为0,输入将是> 0,但(无论您的系统使用来表示一个换行符)。 如果将其更改为

if ((input < 0) || (length == 0)) return null;

如果任一输入流被关闭或进入一个空行的方法将返回空值。的强文本

相反!leido.equals(" ")的,简单地使用!leido.equals("")。前者终止当输入仅含有一个空间中的线,后者被输入一个空行的时候。

我不明白很多你的问题,而只是一个建议,从我身边将是,以这种方式使用条件

while(!" ".equals(leido)){  
//instead of   
while (!leido.equals(" ")){

为什么,因为你的代码将抛出一个异常,如果您的变量包含空值,而我提到的条件永远不会做到这一点:P

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top