hibby jibby 123456789

时间:2013-03-15 18:30:55

标签: php

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

如果无法找到,我无法让该方法返回原始单词    它的缩写。通过它的外观,该方法没有达到我的最终回报声明和    我不确定为什么。

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,8

9,9

在中,u

2 个答案:

答案 0 :(得分:1)

你应该改变循环条件:

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

到此:

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

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

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

编辑:我看到你正在努力提出一个好问题,但你仍然无法提供可编译的代码。所以我会尽力帮助你。实际上,这段代码对于它想要实现的东西来说太复杂了。你想要的更像是这样:

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

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

答案 1 :(得分: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;
}