gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:39:24 34 4
gpt4 key购买 nike

我正在开发一个单词搜索项目,该程序获取一个文本文件作为输入,第一个方法 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com