This is NOT only about upper and lowercases within a string. It is about every other character that isn't a whitespace nor a letter!
I am trying to get the exact numbers of uppercase letters, lowercase letters, whitespaces and other characters for a textfile that i am reading. However i am not getting the exact values that i searching for. The only thing that is actually giving me the exact values everytime is the lowercases.
I do know that depending on which characters that i compare it too i get different results. But i don't know by which characters things go from and from which they end. Tried to look at the ASCII table for answers but it is not enough.
Have tried in this way, the code below, and with if statements that test for characters within an array that goes by a forloop.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class test {
public static void main(String args[]) throws IOException {
FileReader file = new FileReader("C:\\Users\\karwa\\Desktop\\kq.txt");
BufferedReader read = new BufferedReader(file);
int upper = 0, lower = 0, number = 0, special = 0;
boolean ch;
while (ch = read.read() != -1) {
for (char c : read.readLine().toCharArray()) {
char ch1 = c;
if (ch1 >= 'A' && ch1 <= 'Z')
upper++;
else if (ch1 >= 'a' && ch1 <= 'z')
lower++;
else if (ch1 >= '0' && ch1 <= '9' || ch1 >= '!' && ch1 <= '-')
number++;
else
special++;
}
}
System.out.println("Upper case letters : " + upper);
System.out.println("Lower case letters : " + lower);
System.out.println("Whitespaces : " + special);
System.out.println("Others : " + number);
}
}