checking for is letter in a string

元气小坏坏 提交于 2019-12-24 00:35:33

问题


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?


回答1:


In isLetter, L should be lowercase. (l).

if (isLetter(s.charAt(i) ) )
   // ^ lowercase 

Keeping that mistake aside, why are you recursively calling the method isletter in the loop.




回答2:


Maybe you mean to use the isLetter() method of the Character class like this:

 if(Character.isLetter(s.charAt(i))){

  }



回答3:


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.




回答4:


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.




回答5:


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.




回答6:


I'll assume for the moment that the following code is defined in a main method:

Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();

isletter(s);  // a call to the function

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.




回答7:


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 the Character Class not the String Class. You must use Character at the beginning because you are using a member function of the Character class. If you used an object such as a, b or whatever your character variable is it 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.




回答8:


public class Demo {

public static void main(String[] arg) {

    char st[] = { 'o', '2', 'A', 'b' };

    for (int i = 0; i < st.length; i++) {
        if (Character.isLetter(st[i])) {
            System.out.println("Character At:" + i + "\t" + st[i]);

        }

        else {
            System.out.println("Integer At:" + i + "\t" + st[i]);

        }
    }

}

}

isLetter() method belongs to Character array.



来源:https://stackoverflow.com/questions/5852744/checking-for-is-letter-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!