Reputation: 85
If I have a list containing [StudentName, ResigsterNumber, Department, grade , comment]
How can I choose only those words which starts with small letter. i.e. (grade and comment)
Upvotes: 0
Views: 100
Reputation: 103
for (String l : list){
char character = l.charAt(0); // This gives the first character
if (Character.isLowerCase(character)){
// proceed with list element with small letter found
}
}
Upvotes: 0
Reputation: 4378
With Java 8 it is pretty easy:
List<String> list = Arrays.asList("StudentName", "Department", "grade", "comment");
List<String> filteredList = list.stream()
.filter(s -> Character.isLowerCase(s.charAt(0)))
.collect(Collectors.toList());
System.out.println(filteredList); // prints "[grade, comment]"
Upvotes: 2
Reputation: 1
Just loop through the elements and put them through the following condition:
boolean hasLowerCase = element.equals(element.toLowerCase());
This will allow you to check:
if(hasLowerCase){System.out.println(Element is in lowercase)};
if(!hasLowerCase){System.out.println(Element is not in lowercase)};
Upvotes: 0
Reputation: 896
Here is the full class with the final output printed:
public class Test {
public static void main(String[] args) {
String[] testNames = new String[] { "StudentName", "ResigsterNumber", "Department", "grade", "comment" };
List<String> names = new ArrayList<>();
Arrays.asList(testNames).forEach((testCh) -> {
if (testCh != null && !testCh.isEmpty() && Character.isLowerCase(testCh.charAt(0))) {
names.add(testCh);
}
});
System.out.println(names);
}
}
Upvotes: 0
Reputation: 792
Just iterate through the list and select the strings whose 1 element lies between 'a'
and 'z'
List<String> result=new ArrayList<String>();
for(String str: list){
if(str.charAt(0)>='a' && str.charAt(0)<='z'){
result.add(str);
}
}
Upvotes: 0
Reputation: 574
You can check the ASCII value of the first letter of each word. The ASCII value of lowercase letters is between 97 to 122.
Loop through the list and store the String in variable str
int i=str.charAt(0);
if(i>=97 && i<=122){
// this is a word starting with lowercase letter
System.out.println(str)
}
Upvotes: 0