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 6 of 6

Thread: what is charAt(0) ?

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is charAt(0) ?

    Is there anybody could tell me what does this function use for?For example :

    String strTemp = JOptionPane.showInputDialog(null,"Please Enter C To Open A Current Account \n Or J To Open A Joint Account");
    switch(strTemp.charAt(0)){
    				case 'c':
    				case 'C':
                                  //code statement
                                   break;
                                  case 'j':
                                  case 'J':
     }

    Basically if i program the statement in c++ ,i would coding it in this form
    switch(strTemp)
    {
                                   case 'c':
    				case 'C':
                                  //code statement
                                   break;
                                  case 'j':
                                  case 'J':
     }

    But it won't work in java ,while i try to compile it an error prompt out mentioned that ("invalid type" in switch(strTemp)) So why ?

  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: what is charAt(0) ?

    Hello.

    The reason for the switch statement not liking "strTemp" being passed in is because you cannot use switch for checking strings. However if you use strTemp.charAt(0) you will get the first character in the string, hence the index zero (0). We can then use the switch to check the character. Switch will take anything represented as a number of int or lower or an enum. So thats byte, short, int, char and enum.

    // Json

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: what is charAt(0) ?

    I think you need to learn C++ better too before making those kind of remarks.

    #include <string>
    #include <iostream>
     
    using namespace std;
     
    int main(void){
        string test = "j";
     
        switch(test){
            case 'j':
                cout << "It worked! Huh... How!";
                break;
            default:
        }
     
        retrun 0;
    }
    this is an epic fail.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: what is charAt(0) ?

    Quote Originally Posted by Freaky Chris View Post
    this is an epic fail.
    lol thats funny
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: what is charAt(0) ?

    wud like to add some question....

    so thats why if i want to locate a character in a string i should measure it first...? by .length() method..
    and because java is measusring strings from 1-(depends on the maximum length) while the index are '0' I have to minus-1 (so it will be 0-(max length), so if i use .charAt(0) i can locate the first character and the index wont bounce out..

    any sense?
    Last edited by chronoz13; October 10th, 2009 at 08:59 PM.

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: what is charAt(0) ?

    Not really

    But here goes, in most cases in Java, the index is zero based. So for instance in the charAt method you will have to use a zero to get the first character. However if you wish to do a substring between x and y only the start position is zero based but the end is not. Not sure if that makes any sense, just have a look at the API reference.

    // Json

  7. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (October 11th, 2009)