Results 1 to 3 of 3
  1. #1
    scottpush is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default How to compare char

    Hello

    I'm trying to read a single character input and then decide what character it actually is. I can only figure out how to read a string input but not just a character input. Also, once I have that character I don't know the syntax to decide if that character is an A or B or whatever.

    Thanks,

    Scott

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can always get a char out of a String by String's charAt(...) method. If the String has length of 1, then charAt(0) will get that single character of the String and make it a char:

    Java Code:
    char c = "h".charAt(0);
    Last edited by Fubarable; 02-27-2010 at 11:25 PM. Reason: oops error corrected

  3. #3
    scottpush is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    Okay thanks,

    Now I need to compare to see if the character is an A or B or...

    This is my code:

    String name = input.nextLine();
    }

    String name;
    // do this if parcel is an envelope
    if ( name == A ) // if result 1,

    This give me an error saying 'name' cannot be resolved. I used the String method above to read the input because my teach suggested so. Can't get this to work now.

Similar Threads

  1. Compare a char
    By Torgero in forum New To Java
    Replies: 3
    Last Post: 02-01-2009, 03:24 PM
  2. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  3. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 07:04 PM
  4. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 03:57 AM
  5. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •