Re: converting String type to char

From: David Heath (barcodedave_at_netscape.net)
Date: 02/22/04


Date: Sun, 22 Feb 2004 00:24:51 GMT

Hi Mark,

Gender is a String type. The original post was because I was going to
try changing the attribute to a char type. After seeing some of the
responses I saw that it wasn't necessary. if(gender.equals("M") worked.

This all started because the compiler didn't throw an error on the line
if(gender == "M")
but the code didn't work. I now understand that == is used to compare
integer or char types.

David

Mark Haase wrote:

> In article <uQvZb.25468$Xp.103864@attbi_s54>,
> David Heath <barcodedave@netscape.net> wrote:
>
>
>>Steve,
>>
>>What I'm trying to achieve is to display "Welcome Mr./Ms. <name>" based
>>on user input to name and gender. This is the code
>>
>> public void welcome()
>> {
>> if(gender == "M") {
>
>
>
> Does it even compile? I should think not. You should have posted the
> error message, but I can explain part of the problem right now.
>
> When you put double quotes around something, that declares it as a
> String literal. So you're comparing a char to a String, which is illegal.
>
> Put single quotes around a char to tell the compiler that its a char
> literal.
>



Relevant Pages

  • Re: converting String type to char
    ... Except that he stated earlier that gender was a char. ... is that you're comparing a char to a String literal. ... The single quote or apostrophe denotes a char. ...
    (comp.lang.java.help)
  • [PATCH 09/21] perf: rewire generic library stuff, p5
    ... +int eprintf(int level, const char *fmt, ...) ... * Helper function for splitting a string into an argv-like array. ... +static int count_argc(const char *str) ...
    (Linux-Kernel)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: Returning a character buffer from a DLL
    ... I need to return a string buffer from the DLL in a RunQuery function. ... I find it odd that you are using the obsolete 'char *' data type here. ... want to use a string pointer of any type here! ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)