检查字符串中是否有字母
我要求用户输入字符串,我想检查是字母还是数字,但我是java新手。这是我到目前为止所得到的
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
isletter(s); // a call to the function
// function
public void isletter(String s)
{
for (int i = 0; i < s.length(); i++)
if (isLetter(s.charAt(i) ) ) {
System.out.println("is alpha = " + s);
}
else{
}
}
这是我在尝试通过 dos 进行编译时遇到的错误
c:\programming>javac LexemesTokenizer4.java
LexemesTokenizer4.java:62: non-static method isletter(java.lang.String) cannot b
e referenced from a static context
isletter(s);
^
LexemesTokenizer4.java:71: non-static method isletter(java.lang.String) cannot b
e referenced from a static context
isletter(s);
^
LexemesTokenizer4.java:85: cannot find symbol
symbol : method isLetter(char)
location: class LexemesTokenizer4
if (isLetter(s.charAt(i) ) ) {
^
3 errors
c:\programming>
我知道这是一个简单的修复吗?
I am asking for user input in a string and i want to check for is alpha or numeric but i am new to java. this is what i have so far
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
isletter(s); // a call to the function
// function
public void isletter(String s)
{
for (int i = 0; i < s.length(); i++)
if (isLetter(s.charAt(i) ) ) {
System.out.println("is alpha = " + s);
}
else{
}
}
Here is the error i am getting when trying to compile through dos
c:\programming>javac LexemesTokenizer4.java
LexemesTokenizer4.java:62: non-static method isletter(java.lang.String) cannot b
e referenced from a static context
isletter(s);
^
LexemesTokenizer4.java:71: non-static method isletter(java.lang.String) cannot b
e referenced from a static context
isletter(s);
^
LexemesTokenizer4.java:85: cannot find symbol
symbol : method isLetter(char)
location: class LexemesTokenizer4
if (isLetter(s.charAt(i) ) ) {
^
3 errors
c:\programming>
I know this is an easy fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在
isLetter
中,L
应该是小写。 (l
)。抛开这个错误不谈,为什么要在循环中递归调用 isletter 方法呢?
In
isLetter
,L
should be lowercase. (l
).Keeping that mistake aside, why are you recursively calling the method
isletter
in the loop.也许您的意思是像这样使用Character类的isLetter()方法:
Maybe you mean to use the isLetter() method of the Character class like this:
您应该使用以下命令导入扫描仪,而不是顶部的
Scanner scanner = new Scanner(System.in);
:导入java.util.Scanner;
在最上面。
Instead of
Scanner scanner = new Scanner(System.in);
on the top you should import a scanner using this:import java.util.Scanner;
on the very top.
尝试使用 s.matches("[A-Za-z]"),而不是函数 isLetter()。
这是一个字符串函数,如果字符串与提供的正则表达式匹配,则重新运行 true。
编辑:您需要更改正则表达式以接受长度值大于一的字符串。
Instead of the function isLetter(), try s.matches("[A-Za-z]").
That is a string function that reruns true if the string matches the provided regular expression.
Edit: you will need to alter your regex to accept strings of lengh values greater than one.
您应该首先将“isletter”定义为静态。
在 Java 中保持大小写为 lowerThenUpper 也是一个好习惯。
除此之外,也许可以看看扎克的回答。
You should first define "isletter" as static.
It's also a good habit to keep casing to lowerThenUpper in Java.
Other than that, see Zach's answer, probably.
我暂时假设以下代码是在 main 方法中定义的:
发生第一个编译错误是因为 main 方法被定义为以下签名: public static void main(String[] args) (也可以使用 varargs在 Java 5+ 中)。
这意味着,由于您的静态方法正在调用 isletter(s),因此 isletter(s) 必须声明为静态 - 因为它不是静态的,所以它属于该类的实例,而不是类本身。静态方法只能调用静态方法,除非它们调用实例上的方法。因此,
非静态方法 isletter(java.lang.String) 不能
从静态上下文引用的 e
意味着 isletter 应该附加一个 static 修饰符:
public static void isletter(String s)
下一个错误是相同的。
最后,方法 isletter(String s) 调用带有字符 (char) 的方法。您尚未定义方法 isletter(char character),也不是静态的 - 因为它是通过静态方法调用的,所以它也必须设为静态。
我希望这能回答您有关编译错误的问题。
I'll assume for the moment that the following code is defined in a main method:
Your first compilation error occurs because the main method is defined as the following signature: public static void main(String[] args) (can use varargs as well in Java 5+).
This means that, since your static method is calling isletter(s), the isletter(s) must be declared as static - since it is not static, it belongs to an instance of the class and not the class itself. Static methods can only invoke static methods unless they are invoking a method on an instance. Thus, the
non-static method isletter(java.lang.String) cannot b
e referenced from a static context
means that isletter should have a static modifier attached:
public static void isletter(String s)
The next error is the same.
Finally, the method isletter(String s) invokes a method with a character (char). You have not defined the method isletter(char character), nor is it static - because it is being invoked via a static method, it too must be made static.
I hope this answers your questions about your compilation errors.
方法 isLetter() 测试字符而不是字符串。首先确保您谈论的是正确的类型。这只是问题的一部分。首先将您的字符串更改为字符,然后回复我们。
另请记住:您必须使用此格式
Character.isLetter(s.charAt(charCount));
这是因为
isLetter()
是的成员方法Character
类而不是String
类。您必须在开头使用
Character
,因为您使用的是Character
类的成员函数。如果您使用了 a、b 等对象或任何字符变量它会告诉您该字符变量不能被取消引用。也许这与
isLetter()
是类方法而不是对象方法这一事实有关。至于静态上下文,它必须与以下事实有关:您的 main 方法被声明为静态,但您的其他方法不是。我无法向你完全解释这一点,但它
只是一个提示。
The method
isLetter()
tests characters not strings as you have there. Make sure you are talking about the right type first of all. This is only part of the problem. Change your string to a character first of all then get back to us.Also remember this: You must use this format
Character.isLetter(s.charAt(charCount));
This is because
isLetter()
is a member method of theCharacter
Class not theString
Class.You must use
Character
at the beginning because you are using a member function of theCharacter
class. If you used an object such as a, b or whatever your character variable isit will tell you that that character variable can not be de-referenced. Perhaps that has something to do with the fact that
isLetter()
is a Class method not an object Method.As for static context it has to do something with the fact that your main method is declared static but your other method is not. I can not fully explain this to you but it
is just a hint.
}
isLetter() 方法属于字符数组。
}
isLetter() method belongs to Character array.