We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,031 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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 ?

4
Contributors
5
Replies
4 Hours
Discussion Span
3 Years Ago
Last Updated
7
Views
Question
Answered
low1988
Light Poster
45 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 107
Skill Endorsements: 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.

nomemory
Light Poster
35 posts since Sep 2009
Reputation Points: 19
Solved Threads: 6
Skill Endorsements: 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)

low1988
Light Poster
45 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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

low1988
Light Poster
45 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Years Ago by JamesCherrill, quuba and nomemory

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0711 seconds using 2.72MB