java - 如何使用主类中的方法打印的输出?

标签 java arrays string methods

我正在开发一个单词搜索项目,该程序获取一个文本文件作为输入,第一个方法 scanVocabulary 创建文本中前 3000 个单词的数组。第二种方法 printWords 获取 scanVocabulary 的输出和三个字母,它返回包含这 3 个字母的单词(按插入顺序排列)。

当我运行我的程序时,我可以看出 scanVocabulary 有效,因为我得到了插入的文本的正确长度,但是当我运行 printWords 时,我没有'根本得不到输出。

我的方法应该打印输出而不是返回它。那么我应该如何获得输出?

我认为问题出在 main 方法上。

这是我的代码:

    package sw1.ex4;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class WordSearch {
    public static String[] scanVocabulary(Scanner scanner) {

        int counter = 0;
        String[] result = new String[3000];
        while (scanner.hasNext() && counter < 3000) {
            String word = scanner.next().toLowerCase();
            if (isAlpha(word) && word.length() > 0
                    && !checkOccurance(word, result)) {
                result[counter] = word;
                counter++;
            }

        }
        if (counter < 3000) {
            result = clean(result);
        }
        return result;

    }

    public static String[] clean(final String[] v) {
        List<String> list = new ArrayList<String>(Arrays.asList(v));
        list.removeAll(Collections.singleton(null));
        return list.toArray(new String[list.size()]);
    }

    public static boolean isAlpha(String name) {
        char[] chars = name.toCharArray();

        for (char c : chars) {
            if (!Character.isLetter(c)) {
                return false;
            }
        }

        return true;
    }

    public static boolean checkOccurance(String word, String[] array) {

        for (String str : array)
            if (str != null) {
                if (str.equals(word))
                    return true;
            }
        return false;
    }

public static void printWords(String[] vocabulary,
        String firstLetter, String secondLetter, String thirdLetter){

    int counter=0;
    for (String str : vocabulary){
        int index1=0;
        int index2=0;
        int index3=0;

        if (str.contains(firstLetter))
            index1=str.indexOf(firstLetter);
        if ((str.substring(index1, str.length())).contains(secondLetter))
            index2=str.indexOf(secondLetter);
        if ((str.substring(index2, str.length())).contains(thirdLetter))
            index3=str.indexOf(thirdLetter);
        if (index3>index2 & index2>index1){
            System.out.println(str);
            counter++;
        }
        else
            System.out.println(index1 +"," + index2 +","+index3);

    }
    System.out.println("found "+ counter+" words");

}




public static void main(String[] args) throws FileNotFoundException{
    File theFile = new File(args[0]);


    Scanner s = new Scanner(theFile);
    String[] wordslist = scanVocabulary(s);
    System.out.println("Read " +wordslist.length+" words from "+args[0]); //check if args[0] is ok
    System.out.println("Enter 3 letters or exit");
    Scanner input = new Scanner(System.in);
    while (input.hasNextLine()) {
        String line = input.nextLine();
         if(line.equals("exit")) 
                break;

        if (line.length()!=5)
            System.out.println("[WARNING] Expecting 3 letters separated by one space");
         if (Character.isLetter(line.charAt(0)) && Character.isLetter(line.charAt(2)) && Character.isLetter(line.charAt(4)) )
             continue;
         else
             System.out.println("[WARNING] Expecting 3 letters separated by one space");

         String first = String.valueOf(line.charAt(0));
         String second = String.valueOf(line.charAt(2));
         String third = String.valueOf(line.charAt(4));

        printWords(wordslist,first,second,third);

    }
    input.close();

}

}

最佳答案

首先,为什么你必须循环遍历字符串并检查字符是否存在,而你有许多预定义的方法可以完成这项工作,请看一下:

  1. .contains() Method
  2. .indexOf() Method

另外最好使用 char.compareTo(char) method 比较两个字符,因此更改此:

if (firstLetter.charAt(0)==letters[0])

具有以下内容:

if (firstLetter.charAt(0).compareTo(letters[0]))

编辑:

更改您的测试并直接比较indexof firSTLetter、indexof secondletter和index of thirdletter,如下所示:

    if (str.contains(firstLetter)) {
        index1=str.indexOf(firstLetter);
        if (str.contains(secondLetter) && str.indexOf(secondLetter)>index1){
           index2=str.indexOf(secondLetter);
           if (str.contains(thirdLetter) && str.indexOf(thirdLetter)> index2)        {
               System.out.println(str);
               counter++;
           }
        }
    }

请注意,AND 运算符在 java 中是 &&,而不是 &

编辑2:

为了保证在输入两次的情况下能够找到该字母的其他出现,可以将找到的字母替换为0,这样如果下一个字母相同,则为第一次出现将被删除并且找不到,因此如果再次发生,您可以获得下一个索引:

if (str.contains(firstLetter)) {
        index1=str.indexOf(firstLetter);
        str=str.substring(0,index1)+'0'+str.substring(index1+1);
        if (str.contains(secondLetter) && str.indexOf(secondLetter)>index1){
           index2=str.indexOf(secondLetter);
           str=str.substring(0,index2)+'0'+str.substring(index2+1);
           if (str.contains(thirdLetter) && str.indexOf(thirdLetter)> index2)        {
               System.out.println(str);
               counter++;
           }
        }
    }

关于java - 如何使用主类中的方法打印的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29667283/

相关文章:

Java 应用程序有 10% 的时间崩溃

java - 读取父类中的子类变量

java - 如何从字符串中删除尾随零

java - 在操作栏选项卡中添加徽章 View

c++ - 当参数是数组时,C++ 如何开始和结束工作?

php - 使用数据库或公共(public)变量从大数据中选择

xml - XSLT:使用 xsl:value-of - 我返回的是字符串还是单个字符串的节点集?

javascript - 使用 Javascript 按月将 JSON 解析为表格

python - 将单词中的字母频率与 R(或 python)中的 26 个字母进行匹配

java如何从末尾拆分字符串