1,105,399 Community Members

What is charAt(0) ?

Member Avatar
low1988
Light Poster
45 posts since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 ?

Member Avatar
quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 81 [?]
Q&As Helped to Solve: 107 [?]
Skill Endorsements: 0 [?]
 
-1
 

first is OK

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
                System.out.println("cC");
                break;
            case 'j':
            case 'J':
                System.out.println("jJ");
                break;
            default:
                System.out.println("no(cC,jJ)");
        }

Always add :default:" steatment. This is a practical.

Member Avatar
nomemory
Light Poster
31 posts since Sep 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

As you can see the JOptionPane method returns a String.
You cannot apply switch on String, so you must somehow "convert" that String in char. The charAt() method returns the first char in a String - exactly what you need.

In C++ the String is probably already a char* :), that's why it works.

Note that there's a slight difference between Java's String and C++ String.

Member Avatar
low1988
Light Poster
45 posts since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

As you can see the JOptionPane method returns a String.
You cannot apply switch on String, so you must somehow "convert" that String in char. The charAt() method returns the first char in a String - exactly what you need.

In C++ the String is probably already a char* :), that's why it works.

Note that there's a slight difference between Java's String and C++ String.

So ,it is all about convert the string into char...But i'm still confuse about "0" parameter in charAt(0).eg.i had enter 'c' as requested by the JOptionPane dialog ,so how the switch statement
verifies that the value i had entered is 'c' with the only '0' parameter inside the charAt(0)

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

So ,it is all about convert the string into char

Not exactly. charAt is about extracting a particular single char from a String.
The chars in a Java String are numbered from 0 to (length of String -1).
charAt(n) gives you the char in position n in the String.
So if the String is "Hello", charAt(0) is 'H', charAt(1) is "e", charAt(4) is 'o'.

You need to extract a single char in this case because you can't do a switch on a String value.

Member Avatar
low1988
Light Poster
45 posts since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Not exactly. charAt is about extracting a particular single char from a String.
The chars in a Java String are numbered from 0 to (length of String -1).
charAt(n) gives you the char in position n in the String.
So if the String is "Hello", charAt(0) is 'H', charAt(1) is "e", charAt(4) is 'o'.

You need to extract a single char in this case because you can't do a switch on a String value.

Thanks ,it does a lot of help

Question Answered as of 4 Years Ago by nomemory, JamesCherrill and quuba
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article