import java.util.Scanner;
public class ValidString {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the String: ");
//String s = scan.nextLine();
StringBuilder str = new StringBuilder(scan.nextLine());
System.out.println(isValidString(str));
scan.close();
}
public static boolean isValidString(StringBuilder str) {
for(int i = 0;i<str.length();i++) {
int s = (int)newStr.charAt(i);
if(s>=97 && s<=122 || s>=65 && s<=90){
continue;
}else {
return false;
}
}
return true;
}
}
I was expecting the output as true if string contains only Alphabets else false value. Can anyone trace out what's the problem with this code.
str.charAt(i)<='A' && str.charAt(i)>='Z' && str.charAt(i)<='a' && str.charAt(i)>='z')is never satisfied.if (x <= 5 && x >= 10 && x <= 25 && x >= 30). What value would you expect to satisfy all those parts? (I'd also suggest that if you callstr.charAt(i)once and store the result in a local variable to use in your condition multiple times, your condition is likely to end up being more readable.)xis both "less than or equal to 5" and "greater than or equal to 10"? Then apply that same thinking to character comparisons.isValidString(StringBuilder str)is supposed to returntrue. Pretend you are the computer, and follow the logic of that method. Use pen and paper for your computer memory. Create truth tables with columns such asc <='A',c>='Z', and so on.c, in my previous comment, represents the current value pointed to bystr.charAt(i).