Re: converting String type to char

From: Ryan Stewart (zzanNOtozz_at_gSPAMo.com)
Date: 02/21/04


Date: Fri, 20 Feb 2004 17:13:09 -0600


"Andrew Thompson" <SeeMySites@www.invalid> wrote in message
news:_4wZb.68624$Wa.67332@news-server.bigpond.net.au...
> David Heath 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") {
>
> You should use this form for String comparison..
>
> if(gender.equals("M")) {
>
> > System.out.println("Welcome Mr. " + name);
> > }
> > else{
> > System.out.println("Welcome Ms. " + name);
> > }
> > }
>
Except that he stated earlier that gender was a char. That should've been
included in this code sample. If that is still the case, then your problem
is that you're comparing a char to a String literal. You want:
if (gender == 'M') { ... }

The single quote or apostrophe denotes a char. Double quotes are always
String objects.



Relevant Pages

  • Re: char x=x; why not char x="x"; ?
    ... value which is not implicitly carried into a char bacause it's a ... single quote have some meaning that understanding may assist in making ... single quotes are used to define a char literal and double quotes a string literal; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: char x=x; why not char x="x"; ?
    ... I'm guessing double-quotes is incorrect because it signifies a string ... single quote have some meaning that understanding may assist in making ... where Dim c As Char = "x"C is necessary when Strict is ... on instead of Dim c As Char = "x"). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: converting String type to char
    ... Gender is a String type. ... try changing the attribute to a char type. ...
    (comp.lang.java.help)
  • how to double single quote (beginner blockage)
    ... if I have a string that contains single quote (f.e. ... "al'alamein") i would like to escape it with ... fixed_value << char ...
    (comp.lang.ruby)
  • [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)