1

我目前正在尝试创建一个可以使用存储在外部文本文件中的缩写词来缩短单词的应用程序。单词后面可以跟标点符号,缩短后也应该保留。

如果找不到它的缩写,我无法让该方法返回原始单词。从表面上看,该方法没有达到我的最终退货声明,我不确定为什么。

public class WordShortener {


private Scanner fileIn;
private String shortenedWord ="";
private String s="";

    public WordShortener() throws Exception {
    // to be completed
    Scanner fileIn = new Scanner(new File("abbs.txt"));
    this.fileIn = fileIn;
    }

public String wordShortener( String WordToShorten ) {
    // to be completed
        String s = wordToShorten;
            String shortened ="";
        while ( shortenedWord.equals(WordToShorten) && fileIn.hasNextLine()  ) {
            String line = fileIn.nextLine();
            String punctuationChar="";
            String[] lines = line.split(",");
            String originalWord = lines[0];
            String shortenedWord = lines[1];
            String punctuations = "'?.";

            if ( punctuations.contains(s.substring(s.length() - 1)) ) {
                punctuationChar = s.substring(s.length() - 1);
            }

            if ( wordToShorten.contains(lines[0]) ) {
                String shortenedWord = s.replaceAll(wordToShorten, lines[1]);
                shortenedWord = shortened + punctuationChar;
            }
        }
        if ( shortenedWord == wordToShorten ) {
            return wordToShorten;
            }
fileIn.close();
return shortenedWord;
}

this is the otherfile that is used to conduct the shortenWord message on a given word(I have imported java util and java io in the file on my computer:



public class cmdShortener {


    public static void main(String[] args) throws Exception {
        WordShortener WS = new WordShortener();
        String return = WS.shortenWord("hello");
        System.out.println( "Shortened Message:" + return );
        }
}

用逗号分隔的缩写文件中的一些示例行(这些将不会被编辑):

八,8

九,9

你,你

4

2 回答 2

1

您应该更改循环条件:

while ( shortenedWord.equals(inWord) | fileIn.hasNextLine()  )

对此:

while ( shortenedWord.equals(inWord) && fileIn.hasNextLine()  )

事实上,只要您没有找到缩写或文件中还有剩余的行,循环就会继续。如果你没有更多的行,也没有找到缩写,那么它就永远不会结束。

此外,您应该始终使用逻辑运算符 ( &&and ||) 而不是按位运算符 ( &and |),因为如果没有必要,前者不会计算表达式的其余部分。

编辑:我看到您正在努力提出一个好问题,但您仍然无法提供可以编译的代码。因此,我将尽力帮助您解决我所拥有的一切。实际上,这段代码对于它试图实现的目标来说太复杂了。你想要的更像是这样的:

private static final String PUNCTUATIONS = "'?.!;";

public String shortenWord( String inWord ) {

    String originalWord = inWord; // keep track of original input

    // Step 1: get punctuation char and trim inWord to what is needed
    String punctuationChar = "";
    int lengthMinus1 = inWord.length() - 1;
    if (PUNCTUATIONS.contains(inWord.substring(lengthMinus1))) {
        punctuationChar = inWord.substring(lengthMinus1);
        inWord = inWord.substring(0, lengthMinus1);
    }

    while (fileIn.hasNextLine()) {
        // Step 2: get the parts from the line.
        String line = fileIn.nextLine();
        if (line.isEmpty()) {
            continue;
        }
        String[] parts = line.split(",");

        // Step 3: check that inWord is the left part
        if (inWord.equals(parts[0])) {
            // Step 4: return the result
            return parts[1] + punctuationChar;
        }
    }

    // Step 5: if nothing is found, return original.
    fileIn.close();
    return originalWord;
}   

我希望它是不言自明的:)

于 2013-03-15T18:37:26.813 回答
0

首先,不要Short在代码中用作变量名,因为 Short 本身就是包中的一个类java.lang。这是旁注。现在您的方法在此块中返回空字符串:

if ( inWord.contains(parts[0]) ) {
            String Short = s.replaceAll(inWord, parts[1]);
            shortenedWord = Short + punctuationChar;
                                return shortenedWord;//This is returning blank
        }

这可能是因为s您的代码中的值是空白的。!!

编辑
按照您的评论观点,我认为您需要以下方法代码shortenWord

public String shortenWord( String inWord ) 
{
    String punctuations = "'?.!;"; 
    if (punctuations.contains(String.valueOf(inWord.charAt(inWord.length() - 1)))
    {
        while (fileIn.hasNextLine())
        {
            String read = fileIn.nextLine();
            String parts[] = read.split(",");
            if (parts[0].equals(inWord.substring(0,inWord.length() - 1))))
            {
                return parts[1];
            }
        }

    }  
  fileIn.close();  
  return inWord;
}
于 2013-03-15T18:46:55.853 回答