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 ?

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.

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.

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)

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.