checking for is letter in a string
37,034
Solution 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.
Solution 2
Maybe you mean to use the isLetter() method of the Character class like this:
if(Character.isLetter(s.charAt(i))){
}
Solution 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.
Author by
Admin
Updated on June 24, 2020Comments
-
Admin almost 4 years
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?
-
rlc about 13 yearsto wit: the L in
if (isLetter(s.charAt(i) ) ) {