Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Cannot covert boolean to char.

  1. #1
    Member
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot covert boolean to char.

    Real title : Cannot convert char to boolean!

    Hello.

    This code is meant to be able to take any text and count the uppercase letters, lowercase letters, whitespaces and digits (well not really digits but the other characters that are not letters or whitespaces).

    However it seem like i cannot check for up/low case letters by my if statements.

    Any ideas?
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class RaknaTecken {
     
    	private static int upCase = 0;
    	private static int lowCase = 0;
    	private static int digit = 0;
    	private static int whitespace = 0;
     
    	public static void main(String[] args) throws IOException {
    		check();
    	}
     
    	private static void check() throws IOException {
    		System.out.println(
    				"This program checks for different types of letters (upper and lower case), whitespaces and characters.\n");
    		String call = "C:\\Users\\karwa\\Desktop\\kq.txt";
    		FileReader file = new FileReader(call);
    		BufferedReader reading = new BufferedReader(file);
    		System.out.println("Reads file from : " + call + ".\n");
    		try {
    			for (char c : reading.readLine().toCharArray()) {
    				if (Character.toUpperCase(c)) { //Here is the first error. 
    					upCase++;
    				} else if (Character.toLowerCase(c)) { //Here is the second error. 
    					lowCase++;
    				} else if (Character.isDigit(c)) { 
    					digit++;
    				} else if (Character.isWhitespace(c)) {
    					whitespace++;
    				}
    			}
    		} catch (Exception e) {
    			System.out.println("Error : " + e.getMessage());
    		}
     
    		System.out.printf("The text file contains : %d upCase, %d lowCase, %d digit, and %d whitespace characters.\n",
    				upCase, lowCase, digit, whitespace);
    	}
    }

    Also i know that i have a try/catch and IOException but just playing around with those two atm.

    Here is the error:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Type mismatch: cannot convert from char to boolean
    	Type mismatch: cannot convert from char to boolean
     
    	at km222nb_lab4.RaknaTecken.check(RaknaTecken.java:28)
    	at km222nb_lab4.RaknaTecken.main(RaknaTecken.java:16)
    Last edited by MrLowBot; January 2nd, 2019 at 08:01 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot covert boolean to char.

    Type mismatch: cannot convert from char to boolean
    What are the if statements supposed to be testing.
    An if statement requires a boolean value not a char value. Change the expression in the if statement to return a boolean value.
    For example: x > 2 is an expression that returns a boolean value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot covert boolean to char.

    Quote Originally Posted by Norm View Post
    What are the if statements supposed to be testing.
    An if statement requires a boolean value not a char value. Change the expression in the if statement to return a boolean value.
    For example: x > 2 is an expression that returns a boolean value.
    It is meant to test for up/low cases, digits and whitespaces within a text file.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot covert boolean to char.

    meant to test for up/low cases
    Read the API doc to see what methods to use for that test.
    http://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot covert boolean to char.

    I actually almost got it working with as little code as possible.
    However it is only giving me the exact correct numbers for the lowercase letters and not the rest.

    Looked at API for a while. I think i found another way of testing between characters with the ACSII table.
    However this code is almost working.


    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);
    	}
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot covert boolean to char.

    not the rest.
    What does that mean? Can you post something that shows what you are talking about?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot covert boolean to char.

    Quote Originally Posted by Norm View Post
    What does that mean? Can you post something that shows what you are talking about?
    The question is :
    Write a program CountChar.java, counting characters of different types in a text read from a file. Give the number of characters of the following types:
     
        Upper case letters
        Lower case letters
        "Whitespace" (i.e. space, tab, return)
        Other characters 
     
    The path to the text file to read can be coded into the program. (We will test with some other file.) An example of a text file is HistoryOfProgramming. It is a part of an article from Wikipedia about the history of programming. However, we strongly recommend you to start testing your program with a smaller file where you can manually check the result.
     
    An execution with the file HistoryOfProgramming as input should give the following result:
     
    Number of upper case letters: 86
    Number of lower case letters: 3715
    Number of digits: 41
    Number of "whitespaces": 728
    Number of others: 111
     
    If your result does not agree completely with the example above you have to add a written explanation, why you think this happens, to your submission.

    And here is my code so far :
    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\\pa.txt");
    		BufferedReader read = new BufferedReader(file);
    		int upper = 0, lower = 0, other = 0, whitespace = 0, digits = 0;
    		boolean ch;
    		while (ch = read.read() != -1) {
    			for (char c : read.readLine().toCharArray()) {
    				if (Character.isUpperCase(c))
    					upper++;
    				else if (Character.isLowerCase(c))
    					lower++;
    				else if (Character.isWhitespace(c))
    					whitespace++;
    				else if (Character.isDigit(c))
    					digits++;
    				else
    					other++;
    			}
    		}
    		System.out.println("Upper case letters : " + upper);
    		System.out.println("Lower case letters : " + lower);
    		System.out.println("Digits : " + digits);
    		System.out.println("Whitespaces : " + whitespace);
    		System.out.println("Others : " + other);
    	}
    }

    "If your result does not agree completely with the example above you have to add a written explanation, why you think this happens, to your submission."
    I am almost getting the correct answer, almost. However to get to the correct answer i need just a few more whitespaces.
    And to even get to the correct answer i actually needed to make a whitespace before everyline in my text doc, otherwise it wouldn't add the
    uppcase letters that begins in every new line.

    Here is the link for the text.
    http://homepage.lnu.se/staff/jlnmsi/...rogramming.txt
    Last edited by MrLowBot; January 2nd, 2019 at 11:47 AM. Reason: Adding a link.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot covert boolean to char.

    Do you have any questions or problems with the project?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot covert boolean to char.

    Quote Originally Posted by Norm View Post
    Do you have any questions or problems with the project?
    Two questions.

    1. Why do i need to put a space at the start of every new line? Ideas?

    2. Why do i not get the value of whitespaces that i am meant to get?
    I get 701 but i should get 728. Ideas?

    Thx.
    Last edited by MrLowBot; January 2nd, 2019 at 12:51 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot covert boolean to char.

    1. Why do i need to put a space at the start of every new line?
    Who or what said that? Where are the lines in question? There isn't a technical need to have a leading space on a new line.

    Why do i not get the value of whitespaces that i am meant to get?
    Are all the characters in the input file being counted?
    Do all the other counts match what is expected?
    Is there any count that correlates with the missing value? (728-701 = 27)
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot covert boolean to char.

    Quote Originally Posted by Norm View Post
    Who or what said that? Where are the lines in question? There isn't a technical need to have a leading space on a new line.


    Are all the characters in the input file being counted?
    Do all the other counts match what is expected?
    Is there any count that correlates with the missing value? (728-701 = 27)
    1. I had to test the text a lot to notice that everytime i did put a whitespace before any new line within a text that had a uppercase letter in the begining was
    taking into a count but without doing so it did not count those uppercase letters. That's odd..

    2. All the other characters, except for the whitespaces, are "right" (speaking apart from the fact that i have to put a whitespace before every capital letter on evey new line).
    Everything else match what is expected.

    There is no correlation between them, nor have i found any correlations between them (728 - 701 = 27).

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot covert boolean to char.

    everytime i did put a whitespace before any new line within a text that had a uppercase letter in the begining was
    taking into a count but without doing so it did not count those uppercase letters
    That says the program was reading and ignoring the first character on every line.
    Look at all the places in the code where data is read from the file to see if there are any where the character that was read was not counted. Especially look at any that related to a new line being read.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot covert boolean to char.

    Quote Originally Posted by Norm View Post
    That says the program was reading and ignoring the first character on every line.
    Look at all the places in the code where data is read from the file to see if there are any where the character that was read was not counted. Especially look at any that related to a new line being read.
    Very true. Trying to figure it out atm.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot covert boolean to char.

    Have you compared the size of the file in bytes from the OS's properties with the total of all the counts that are expected?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    74
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot covert boolean to char.

    Quote Originally Posted by Norm View Post
    Have you compared the size of the file in bytes from the OS's properties with the total of all the counts that are expected?
    No and tbh i don't think that i could do that haha, i don't know how to do it...

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot covert boolean to char.

    i don't know how to do it.
    On Windows, RightClick on the file, Select Properties, Look at the display - It shows the number of bytes in the file (Size) and the number of bytes the file uses on disk (Size on disk).

    For the expected count, use a calculator and add all the values given in post#7
    Number of upper case letters: 86
    Number of lower case letters: 3715
    Number of digits: 41
    Number of "whitespaces": 728
    Number of others: 111
    86 + 3715 + 41 etc

    Compare those two values to see if the expected count matches the actual size.

    One consideration is the line-end characters: "\r" and "\n" There can be two of then at the end of every line.
    If there are 15 lines, 14 of them can have the 2 line-end characters: 14 * 2 = 28 which is near the 27 count you are looking for.



    Did you find where the program reads the extra character that is not counted?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. help with char
    By syphethia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2014, 05:38 PM
  2. Creating a number char by char
    By Herah in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 23rd, 2013, 04:59 AM
  3. Replies: 3
    Last Post: March 23rd, 2013, 08:20 PM
  4. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 10:04 AM
  5. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM