用java编写应用程序,分析字符串,分别输出字符串的单词,并统计单词个数

用java编写应用程序,分析字符串,分别输出字符串的单词,并统计单词个数,第1张

public class StringTest {\x0d\ public static void main(String[] args) {\x0d\ //定义一个测试字符串\x0d\ String testStr = "this is a test string";\x0d\ \x0d\ //将该字符串按空格分解成单词数组\x0d\ String[] words = testStrsplit(" ");\x0d\ \x0d\ //输出单词个数\x0d\ Systemoutprintln("该字符串一共有" + wordslength + "个单词!");\x0d\ \x0d\ //输出每一个单词\x0d\ for (int i = 0; i 回答于 2022-12-11

public class VariableTypeResolver {

private final String symbol;

private final ASTNode minScope;

private boolean methodLevel = true;

private boolean typeLevel = true;

/

The found result

/

private SimpleName declSN;

private final ASTVisitor visitor = new ASTVisitor() {

@Override

public boolean visit(SimpleName sn) {

if (found()) {

return false;

}

if (sngetIdentifier()equals(symbol) && sngetParent() instanceof VariableDeclaration) {

declSN = sn;

return false;

}

return true;

}

};

/

Starts resolving with the requested symbol

@param varSymbolNode the variable symbol node to resolve (node must be in the AST)

/

public VariableTypeResolver(SimpleName varSymbolNode) {

thissymbol = varSymbolNodegetIdentifier();

thisminScope = varSymbolNode;

}

public VariableTypeResolver(String varSymbol, ASTNode minScope) {

thissymbol = varSymbol;

thisminScope = minScope;

}

public VariableTypeResolver disableMethodLevel() {

methodLevel = false;

return this;

}

public VariableTypeResolver disableTypeLevel() {

typeLevel = false;

return this;

}

/

Node's parent is instance of {@link VariableDeclarationFragment} or {@link SingleVariableDeclaration}

@return the SimpleName node of declaration

/

public SimpleName resolveDeclSimpleName() {

if (!found()) {

resolve();

}

return declSN;

}

private void resolve() {

if(found()) {return;}

if (methodLevel) {

apply(FindUppermethodScope(minScope));

}

if(found()) {return;}

if (typeLevel) {

AbstractTypeDeclaration typeScope = FindUpperabstractTypeScope(minScope);

applyInFields(typeScope);

if(found()) {return;}

for (TypeDeclaration superClass : superClasses(typeScope)) {

if(found()) {return;}

applyInFields(superClass);

}

}

}

private boolean found() {

return declSN != null;

}

private void apply(ASTNode scope) {

if (scope == null) {

throw new NullPointerException();

}

scopeaccept(visitor);

}

private void applyInFields(AbstractTypeDeclaration typeScope) {

for (Object bd : typeScopebodyDeclarations()) {

if (bd instanceof FieldDeclaration) {

apply((ASTNode) bd);

}

}

}

private List<TypeDeclaration> superClasses(AbstractTypeDeclaration atd) {

if (atd instanceof TypeDeclaration) {

return AstUtilssuperClasses((TypeDeclaration) atd);

}

else {

return CollectionsEMPTY_LIST;

}

}

}

也是网上找的

实际是两个问题:控制台命令解析和文件 *** 作。

建议分成两块,代码结构更清晰,也有利于检查思路。

命令解析这种简单的就要字符串 *** 作检查就行了,以后要做更复杂的可以用词法分析程序来作。

文件 *** 作部分设计个接口,包括CRUD方法,以及必要的参数,每个方法里分别对文件进行遍历查找对应数据进行修改。更好的做法是把用户数据放在数据库里。

以上就是关于用java编写应用程序,分析字符串,分别输出字符串的单词,并统计单词个数全部的内容,包括:用java编写应用程序,分析字符串,分别输出字符串的单词,并统计单词个数、java语义分析、java中的crud,程序怎么写,求赐教。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9506178.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存