0

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);
}
}
  • what does the ` || ch1 >= '!' && ch1 <= '-'` part of your last if condtition? – aBnormaLz Jan 02 '19 at 15:47
  • 1
    If you have a question where you do not get the expected result, i.e. the number of letters, you should also include what numbers you expect and what numbers you get. This makes answering a lot easier and quicker. Otherwise your question is almost an exact duplicate of https://stackoverflow.com/questions/25224954/how-to-count-uppercase-and-lowercase-letters-in-a-string or even https://codereview.stackexchange.com/questions/77164/counting-the-uppercase-lowercase-numbers-and-special-characters-in-a-string – leonardkraemer Jan 02 '19 at 15:57

1 Answers1

2

I'd suggest using Character.isUpperCase, Character.isLowerCase, Character.isDigit, Character.isWhitespace:

for (char c : read.readLine().toCharArray()) {                  
      if(Character.isUpperCase(c)) upper++;
      else if (Character.isLowerCase(c))lower++;
      else if (Character.isDigit(c)) number++;
      else if (Character.isWhitespace) whitespace++;
      else // anything else....
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Well i mean it is almost spot on. However this is also how i did this the first time around. And it didn't give me the correct answers. – Axwell Smith Jan 02 '19 at 15:54
  • 1
    @AxwellSmith then you'll have to specify exactly what you're seeing as a result and what you expect instead. – Ousmane D. Jan 02 '19 at 15:55
  • Alright. What i am getting on the lowercases are 3715, which is correct. But the uppercases are giving me 77 but they are meant to be 86. Whitespaces are giving me 743 but is meant to give me 728. And the rest i get 109 but i am meant to get 111. – Axwell Smith Jan 02 '19 at 15:59
  • @AxwellSmith if the variable `special` is meant to track whitespaces only then why don't you include a check for whitespace only instead of putting it in the `else` block? look at your print statements `System.out.println("Whitespaces : " + special); System.out.println("Others : " + number); }` you're printing `special` for whitespace and `number` for others..... – Ousmane D. Jan 02 '19 at 16:03
  • Your counts are off, 77 + 743 + 109 = 929 and 86 +728 + 111 = 925. – leonardkraemer Jan 02 '19 at 16:04
  • `while (ch = read.read() != -1) { for (char c : read.readLine().toCharArray()) { char ch1 = c; if (Character.isUpperCase(c)) upper++; else if (Character.isLowerCase(c)) lower++; else if (Character.isDigit(c)) other++; else if (Character.isWhitespace(c)) whitespace++; } }` I have changed it so that everything is named to what it is meant to be. But it still isnt working because we are checking for numbers and not every char that also isnt a number nor a whitespace or letter. – Axwell Smith Jan 02 '19 at 16:11
  • And for some reason my counts are off... I have the same exact text and i know that the number of spaces is equal because of the fact that i got the text from the wedsite that our teachers use so there should be no problem there. I marked everything and copied it in to the text file and that's that. I don't believe it to be the txt's fault. – Axwell Smith Jan 02 '19 at 16:17
  • @AxwellSmith **I cannot see** the data in your file **, therefore, I cannot say if what you're saying is correct or not**. the solution above is what has to be done to identify if a character is either a digit, whitespace, uppercase etc. **I'd suggest you debug the program** and see what's being read. – Ousmane D. Jan 02 '19 at 16:22
  • Well it seems like i cannot check for uppercase values if i don't put a whitespace before each line... and that whitespace isn't even counting in. So odd.. – Axwell Smith Jan 02 '19 at 16:36