incomparable types: char and String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • becks
    New Member
    • Dec 2014
    • 3

    incomparable types: char and String

    Hello,

    please I have a java code that reads from file. I want to use while loop to pick each character at a time into an array for further computation but I get this message 'incomparable types: char and String'. what does it mean?
    Code:
    [
    for (int i = 0; i < bFile.length; i++) {
    	       	System.out.print((char)bFile[i]);
                }
     
    	    System.out.println("Done");
            }catch(Exception e){
            	e.printStackTrace();
            }
            while (((char)bFile[i])!="")   ....//here gives the error
           {
             
          }
    
    ]
    Last edited by Rabbit; Dec 8 '14, 04:27 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You should compare only same with same. That means char with char or string with string.
    The ' ' is an empty space written as character, the " " is an empty space written as string. And "" is an empty string, holding no character at all!
    So you need to change your line 10 to
    Code:
    while (((char)bFile[i])!=' ')
    That will execute the while loop until a space is found.

    Remark: if you intended to loop until next line, you should have used '\n'. If you want to loop until end of string, you must use the length of the array "bFile.leng th" as maximum value, starting from 0.

    Comment

    Working...